简体   繁体   中英

C access struct in struct

A project in C is being forced upon me. I do not have much C knowledge, but I'm assuming the answer is simple.

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

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

Assume s2 is already populated. How do access num from s1? 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 . The -> operator can only be used if you are using a pointer to a struct. You are using a struct directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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