Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
结构中的C访问结构_C_Struct - Fatal编程技术网

结构中的C访问结构

结构中的C访问结构,c,struct,C,Struct,一个C语言的项目正强加给我。我没有太多的C语言知识,但我假设答案很简单 struct s1 { char *text; int num; }; struct s2 { struct s1 vals[5]; int numbers; }; 假设s2已经填充。如何从s1访问num?我想我会做类似的事情 struct s2 temp; //temp is populated somehow, doesn't matter in the case printf("%d\n"

一个C语言的项目正强加给我。我没有太多的C语言知识,但我假设答案很简单

struct s1 {
  char *text;
  int num;
};

struct s2 {
  struct s1 vals[5];  
  int numbers;   
};
假设s2已经填充。如何从s1访问num?我想我会做类似的事情

struct s2 temp;
//temp is populated somehow, doesn't matter in the case
printf("%d\n", temp.vals[0]->num);  

但这不起作用。有什么建议吗?

使用
temp.vals[0].num
。只有在使用指向结构的指针时,才能使用
->
运算符。您正在直接使用结构。

使用
temp.vals[0].num
。只有在使用指向结构的指针时,才能使用
->
运算符。您正在直接使用结构。

“此问题是由一个简单的印刷错误引起的。”VTC“此问题是由一个简单的印刷错误引起的。”VTC