c - 收到警告 : assignment from incompatible pointer type for using pointer to char array in struct

标签 c pointers

我在编译时收到“警告:来自不兼容指针类型的赋值”。我怎么能不得到那个警告?

部分源码如下:

typedef struct
{
    char id[9];
    char fName[9];    
    char lName[9];
    int finalExam;
    int midTerm;
    float quiz1;
    float quiz2;
    float quiz3;
    float totalMark;
} Student;

....
....
....

pointAt = students; // initialize pointer
            float* topMark;
            char* topLname;
            char* topId;
            topMark = &(*pointAt).totalMark;
            topLname = &(*pointAt).lName;
            topId = &(*pointAt).id;
            printf("top guy : %s\n", topLname);
            pointAt += 1;

我在以下位置收到该警告:

topLname = &(*pointAt).lName;
    topId = &(*pointAt).id;

这两行会导致警告,因为它指向 char 数组。我该如何解决这个问题?

最佳答案

&(*pointAt).lName&(*pointAt).idchar (*)[9] 类型,而这些值被分配给的变量是 char * 类型,因此不匹配。

摆脱 & 运算符以获得 char [9] 的表达式,它将衰减为 char *。当你这样做时,使用 -> 运算符来引用结构指针的成员:

topLname = pointAt->lName;
topId = pointAt->id;

关于c - 收到警告 : assignment from incompatible pointer type for using pointer to char array in struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41880610/

相关文章:

c - 树元素未按正确顺序显示

c - 在c中的二维数组中获取字符串输入

C++ 指向派生类对象的基类指针不调用派生等于运算符函数

c - 为什么我的链表修改在此 C 代码中不起作用?

C++指针返回0值

cublas matrix matrix multiplication 在应用于具有多个 GPU 的一个非常长的维度的矩阵时给出内部错误

c - 调查视频损坏的工具/技术——ffmpeg/libavcodec

c - getpeername() 不会返回正确的端口,但会返回正确的远程主机套接字地址 C 语言

c++ - 替换 C++ 指针字符串中的字符?

c - 警告 - 赋值使指针来自整数而不进行强制转换