我试图通过函数中的指针写入颜色数组中的元素。功能是:

void setColor(int color1[3],int color2[3], int *red, int *green, int *blue) {
int redInc = (color2[1]-color1[1])/range;
int greenInc = (color2[2]-color1[2])/range;
int blueInc = (color2[3]-color1[3])/range;

int i = 0;
while (i < range) {
    *(red+i) = color1[1] + i*redInc;
    printf("This is red: %s\n",*(red+i));
    *(green+i) = color1[2] + i*greenInc;
    *(blue+i) = color1[3] + i*blueInc;
    i++;
}
return;}

范围定义为常数21。主回路内部:
int color1[3] = {255,0,0};
int color2[3] = {0,255,0};

int red[21] = {0};
int green[21] = {0};
int blue[21] = {0};

setColor(color1,color2,red,green,blue);

我已经在我的Linux机器上尝试过这个代码,它似乎可以工作,但它在我的树莓Pi上隔离了错误。这就是我试图访问阵列的方式吗?

最佳答案

C是零索引的,这意味着如果数组有3个整数长,则可以使用索引0、1和2访问它
例子:

int ex[3] = {1,2,3};
printf("%d %d %d", ex[0], ex[1], ex[2]);

意志产出:
1 2 3

所以,你需要做的是,检查你的代码,检查你索引数组的位置是否不正确,这看起来像函数“CC”中的所有。

关于c - C:在Raspberry Pi上写入数组指针时出现段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44663547/

10-11 22:13
查看更多