首页 > 代码库 > C语言错误:request for member ‘xxx’ in something not a structure or union

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;

}

C语言错误:request for member ‘xxx’ in something not a structure or union