结构中的C访问结构

时间:2014-03-16 22:36:54

标签: 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", temp.vals[0]->num);  

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

1 个答案:

答案 0 :(得分:5)

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

相关问题