[英]Getting the address of a statically allocated array into a pointer inside a struct
The simplified version of my code is the following: 我的代码的简化版本如下:
struct args {
int p;
int *A;
};
typedef struct sort_args sort_args;
void func(int A[], int p)
{
args new_struct = {&A, p};
args *args_ptr = &new_struct;
}
I'm trying to convert a statically (I think that's the term) allocated array into a pointer, but the compiler keeps throwing these warnings: 我正在尝试将静态分配的数组(我认为这是术语)转换为指针,但是编译器不断抛出以下警告:
warning: initialization makes integer from pointer without a cast [enabled by default] args new_struct = {&A, p, r};
警告:初始化使指针从整数变为无强制转换[默认启用] args new_struct = {&A,p,r};
warning: (near initialization for 'new_struct.p') [enabled by default] warning: initialization makes pointer from integer without a cast [enabled by default] warning: (near initialization for 'new_struct.A') [enabled by default]
警告:(在'new_struct.p'的初始化附近)[默认启用]警告:初始化使指针从整数开始而没有强制转换[默认启用]警告:(在'new_struct.A'的初始化附近)[默认启用]
What am I doing wrong? 我究竟做错了什么?
You got the parameters backwards. 您将参数倒退了。
args new_struct = {&A, p};
=> =>
args new_struct = {p, A};
You need to initialize the members of a struct in exactly the same order as they appear in the structs declaration, or you need to use named syntax like this: 您需要以与结构声明中出现的顺序完全相同的顺序初始化结构的成员,或者您需要使用如下命名语法:
args new_struct = { .A = A, .p = p };
But this is usually only used to improve code clarity with larger structs who have more members. 但这通常仅用于提高具有更多成员的较大结构的代码清晰度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.