gpt4 book ai didi

c - 错误: invalid type argument of '->' - Printing the address of a struct field that is also a struct

转载 作者:行者123 更新时间:2023-11-30 21:15:49 24 4
gpt4 key购买 nike

我有一个这样的结构:

struct my_struct {
struct_a a;
struct_b b;
};

我正在尝试打印 my_struct 变量的地址类型及其字段的地址,ab ,它们也是结构:

void some_function(my_struct *my) {     
//....
printf("my addr: %p - a addr: %p - b addr: %p", my, (void *) &my->a,
(void *) &my->b);
}

int main() {
my_struct my; //my is initialized in a function...
//then, it is passed to some_function(...) bellow...
printf("my addr: %p - a addr: %p - b addr: %p", my, (void *) &my->a,
(void *) &my->b);
some_function(&my);
return 0;
}

我收到以下错误:

error: invalid type argument of '->' (have 'struct my_struct')

我的理解有什么问题吗?

最佳答案

问题实际上出在您的 main 函数中,其中 mystruct my_struct 的实例,因此使用 -> 运算符无效。将 printf 调用更改为使用 .:

int main() {        
my_struct my; //my is initialized in a function...
//then, it is passed to some_function(...) bellow...
printf("my addr: %p - a addr: %p - b addr: %p", (void *)&my, (void *) &my.a,
(void *) &my.b);
some_function(&my);
return 0;
}

另请注意,main 必须返回 int

关于c - 错误: invalid type argument of '->' - Printing the address of a struct field that is also a struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51936201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com