简体   繁体   English

结构中的C访问结构

[英]C access struct in struct

A project in C is being forced upon me. 我现在被迫用C语言开发一个项目。 I do not have much C knowledge, but I'm assuming the answer is simple. 我没有太多的C语言知识,但是我认为答案很简单。

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

struct s2 {
  struct s1 vals[5];  
  int numbers;   
};

Assume s2 is already populated. 假设s2已被填充。 How do access num from s1? 如何从s1访问num? I was assuming I would do something like 我以为我会做类似的事情

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

but that doesnt work. 但这不起作用。 Any suggestions? 有什么建议么?

Use temp.vals[0].num . 使用temp.vals[0].num The -> operator can only be used if you are using a pointer to a struct. 只有在使用指向结构的指针时,才可以使用->运算符。 You are using a struct directly. 您正在直接使用结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM