C错误:C2040:'customerPtr':'int'与'customer *'的间接级别不同

时间:2014-03-16 22:28:15

标签: c struct initialization variable-assignment

对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 *'

不同

我已经阅读了与此相似的其他帖子,但我想我不明白中的答案  与他们的计划有关。

谢谢!

1 个答案:

答案 0 :(得分:4)

此分配错误(在全局范围内不允许分配):

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