gpt4 book ai didi

c - 如何访问 typedef 的二维数组

转载 作者:行者123 更新时间:2023-11-30 18:24:39 29 4
gpt4 key购买 nike

如果我有以下代码

typedef float a[5][2];
a x = {(40,30),(46,30),(56,30),(58,30),(60,30)};

编译成功。我在访问上述数组 x 的元素时遇到问题。

for(i=0;i<5;i++)
{
for(j=0;j<2;j++)
{
printf("\nx[%d][%d]=%f",i,j,*x[z++]);
}
}

以上代码输出

x[0][0]=30.000000
x[0][1]=30.000000
x[1][0]=30.000000
x[1][1]=0.000000
x[2][0]=0.000000
x[2][1]=0.000000
x[3][0]=0.000000
x[3][1]=0.000000
x[4][0]=-1.781255
x[4][1]=0.000000

最佳答案

编译时出现警告

warning: left-hand operand of comma expression has no effect [-Wunused-value]

因为

a x = {(40,30),(46,30),(56,30),(58,30),(60,30)};

a x = {30, 30, 30, 30, 30};

是一样的。

你想要的是

a x = {{40,30},{46,30},{56,30},{58,30},{60,30}};

(大括号而不是圆括号)

或者更好

a x = {{40.f,30.f},{46.f,30.f},{56.f,30.f},{58.f,30.f},{60.f,30.f}};

关于c - 如何访问 typedef 的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789969/

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