C 错误 : C2040: 'customerPtr' : 'int' differs in levels of indirection from 'customer *'

标签 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 *”不同

我读过其他类似的帖子,但我想我不明白
中的答案 与他们的计划有关。

谢谢!

最佳答案

这个赋值是错误的(在全局范围内不允许赋值):

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/

相关文章:

c - 如何使用 SV-DPI 将数组从 C 传递到 SV?

c - 将字符串分配给 int 类型的变量

c - 如何将当前日期的值存储到 su.date 中?

c - 在 C 中初始化 char **string 以在soap结构函数参数中使用

c - 从 uint8_t* 到 uint32_t 的转换无效 - 从 32 位架构迁移到 64 位架构时?

c++ - 并行执行的 OpenCL 验证

C结构数组段错误

C - 无法在我的程序中启动堆栈

c++ - 嵌套结构初始化

c++ - initializer_list 中的未初始化值(编译器错误?)