[英]Accessing a struct within a struct
如果我有類似的東西:
struct foo {
struct bar {
char name[8];
int temp;
} example[100];
};
如果我想知道name
是什么,該如何說數組中的第一個元素?
#include <stdio.h>
struct foo {
struct bar {
char name[8];
int temp;
} example[100];
};
int main(void) {
struct foo my_foo;
printf("%s\n", my_foo.example[0].name);
return 0;
}
我沒有檢查您的代碼是否可以編譯,但是就像這樣:
foo var;
var.example[0].name
struct foo myfoo;
char * the_name;
/* initialize myfoo ... */
the_name = myfoo.example[0].name;
struct foo var;
printf("name -- %s\n", var.example[0].name);
注意:我建議您這樣縮寫您的strcut:
typedef struct foo foo;
struct foo {
struct bar {
char name[8];
int temp;
} example[100];
};
這樣,您將不必像這樣聲明您的var:
struct foo var;
但
foo var;
祝好運 ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.