gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:20 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com