结构指针函数中未声明的变量c

时间:2022-09-13 21:43:54

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.

我还在学习C,我正在做一个练习,我必须编写一个汽车数据库。在main函数中,我声明了一个包含100个指向'carinfo_t'结构的数组。在函数'* createcarinfo'中,应创建一个新的carinfo_t实例。但我得到的问题是'brandOfCar'变量未声明。我真的不明白为什么我收到这条消息,因为编译器应该知道这个变量是结构的一部分,对吧?结构在程序中声明为数据类型,并在此函数的开头初始化指向结构的指针。

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;
}

1 个解决方案

#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.

这是因为您调用了传递给构造函数品牌的变量,而不是brandOfCar。同样,您调用了模型变量模型,而不是modelOfCar。这就是strlen不编译的原因。

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

为了一致性,将变量命名为与结构字段完全相同是一个好主意,并在适当的位置添加const:

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.

另请注意,在C中,您不会强制转换malloc,也不要乘以sizeof(char),所有平台上的标准要求为1。

#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.

这是因为您调用了传递给构造函数品牌的变量,而不是brandOfCar。同样,您调用了模型变量模型,而不是modelOfCar。这就是strlen不编译的原因。

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

为了一致性,将变量命名为与结构字段完全相同是一个好主意,并在适当的位置添加const:

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.

另请注意,在C中,您不会强制转换malloc,也不要乘以sizeof(char),所有平台上的标准要求为1。