gpt4 book ai didi

c - 声明时如何在结构内初始化整数指针?

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:20 25 4
gpt4 key购买 nike

#include<stdio.h>  
struct test_stub
{
int n;
int *array;
char *b;
}
test[3]=
{
{5,{1,2,3,4,5},"abcd"}
};
int main()
{

return 0;
}

但这行不通
我得到的错误是 int 不能用于初始化 int *
如果是字符指针我们可以在""这些之间初始化它

最佳答案

如果你真的想在声明时初始化你的结构对象,使用复合文字:

struct test_stub
{
int n;
int *array;
char *b;
}
test[3]=
{
{5,(int [5]){1,2,3,4,5},"abcd"}
};

或者如果您的数组是固定大小的,请将 array 的类型从 int * 更改为 int [5]:

struct test_stub
{
int n;
int array[5];
char *b;
}
test[3]=
{
{5,{1,2,3,4,5},"abcd"}
};

关于c - 声明时如何在结构内初始化整数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23044961/

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