2

I am still learning C and I am doing an exercise where I have to program a car database. In the main function I declared an array of 100 pointers to 'carinfo_t' structures. In the function '*createcarinfo' a new carinfo_t instance should be created. But I get the problem that the 'brandOfCar' variable is undeclared. I do not really understand why I am getting this message because the compiler should know that this variable is part of the structure, right? The structure is declared as a datatype in the program and a pointer to the struct is initialized in the beginning of this function.

I am sorry if this question has already been asked somewhere. Any help is very much appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <limits.h>

struct carinfo_t
{ 
    char *brandOfCar;
    char *modelOfCar;
    int yearCarWasBuilt;
    float valueOfCar;
};


struct carinfo_t *createCarinfo(char *brand, char *model, int year, float 
value)
{
    struct carinfo_t *newCarInfo=(struct carinfo_t*) malloc(sizeof(struct 
carinfo_t));
    newCarInfo->brandOfCar=(char*)malloc(sizeof(char)*
(strlen(brandOfCar)+1));       

//Message:  error: 'brandOfCar' undeclared (first use in this function)

//function not finished
}


int main()
{
    struct carinfo_t *carbase[100]={};

    return 0;
}
0

1 Answer 1

3

This is because you called the variable passed into your constructor function brand, not brandOfCar. Similarly, you called model variable model, not modelOfCar. That's why strlen does not compile.

It's a good idea to name variables identically to the fields of the structure for consistency, and add const where it is appropriate:

struct carinfo_t *createCarinfo(
    const char *brandOfCar
,   const char *modelOfCar
,   int yearCarWasBuilt
,   float valueOfCar) {
    struct carinfo_t *newCarInfo=malloc(sizeof(struct carinfo_t));
    newCarInfo->brandOfCar=malloc(strlen(brandOfCar)+1);
    ...
}

Also note that in C you do not cast malloc, and do not multiply by sizeof(char), which standard requires to be 1 on all platforms.

Sign up to request clarification or add additional context in comments.

4 Comments

I will also suggest to use strdup (if available) instead of malloc(strlen(...)+1) ...
@KeineLust I love strdup, it saves me a lot of typing. I am reluctant to recommend it for portability reasons, though. I wish it were part of the standard, especially given its simplicity.
true, not 100% portable, but why those functions (that requires dynamic allocation) like strdup, itoa, trim ... are not included in the standard library? just for compatibility with embedded systems?
@KeineLust Here is an entire Q&A dedicated to this topic, with interesting answers on both sides.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.