C 的新手。当我运行讲师给我们的代码时,我总是收到此错误。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
//Create a struct for customers
struct customer
{
char lastName[15];
char firstName[15];
unsigned int customerNumber;
};
struct customer customerRecord;
struct customer *customerPtr;
struct customer other;
customerPtr = &customerRecord;
//typedef struct customer to customer
typedef struct customer customer;
char main()
{
//customerRecord.lastName = Flacco;
}
我得到了错误 错误 C2040:“customerPtr”:“int”的间接级别与“customer *”不同
我读过其他类似的帖子,但我想我不明白
中的答案
与他们的计划有关。
谢谢!
最佳答案
这个赋值是错误的(在全局范围内不允许赋值):
customerPtr = &customerRecord;
你应该改变它:
...
struct customer customerRecord;
struct customer *customerPtr = &customerRecord; /* definition & initialization */
struct customer other;
...
或
struct customer customerRecord;
struct customer *customerPtr; /* definition */
struct customer other;
...
int main()
{
customerPtr = &customerRecord; /* initialization */
...
}
同时 main
的返回类型应该是 int
关于C 错误 : C2040: 'customerPtr' : 'int' differs in levels of indirection from 'customer *' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22443924/