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;
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
blabla444
  • 85
  • 5

1 Answers1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I will also suggest to use `strdup` (if available) instead of `malloc(strlen(...)+1)` ... – David Ranieri Aug 13 '17 at 08:29
  • @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. – Sergey Kalinichenko Aug 13 '17 at 08:34
  • 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? – David Ranieri Aug 13 '17 at 08:40
  • 1
    @KeineLust Here is [an entire Q&A dedicated to this topic](https://stackoverflow.com/q/12984948/335858), with interesting answers on both sides. – Sergey Kalinichenko Aug 13 '17 at 08:44