C語言錯誤:request for member ‘xxx’ in something not a structure or union


今天在編譯一個C語言程序時,對於結構體變量,報出錯誤 Error: request for member ‘xxx’ in something not a structure or union。

經過調試發現是 . 與 -> 搞錯了。

如果它是地址,就在它后邊用 ->,如果它不是地址,就在它后邊就用 .

代碼舉例簡略如下:

#include <stdio.h>

#include <string.h>

typedef struct Test_t

{

  char name[20];

  int age;

}Test_s;

void test(Test_s* mytest)

{

  char* ptr = "hello";

  memcpy(mytest->name, ptr, strlen(ptr));  //mytest是一個結構體指針,因此使用“結構體指針名->成員變量名”來引用變量

  mytest->age = 20;

}

int main()

{

  Test_s tt;

  int ret = 0;

  memset(tt, 0, sizeof(tt));

  test(&tt);

  printf("Name:%s\n", tt.name);    //tt是個結構體變量,因此使用 “結構體名.成員變量” 來引用變量

  printf("Age:%d\n",tt.age);

  return ret;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM