gpt4 book ai didi

c - 如何在c中初始化数组?

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

我已经编写了这段代码,但在编译时我收到消息“无效表达式”对于下面的语句

unsigned short address[] ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/*array is being   initialized here.no problem uptil here.*/
unsigned short rowHead=3;
unsigned short colHead=0;
unsigned short rowTail =0;
unsigned short colTail =0;
unsigned short foodx;
unsigned short foody;
unsigned short turn=0;
void gameOver()
{
unsigned short i=0;
address[] ={0b10000001,0b01000010,0b00100100,0b00011000,
0b00011000,0b00100100,0b01000010,0b10000001};
/*this is the line where compiler complaints as invalid expression.*/
while(1)
{
PORTD &=~(1<<i);
PORTB =address[i];
delay_ms(5);
PORTD |=1<<i;
i%8;
}
}

最佳答案

您只能在同时声明和分配变量时这样做。你以后不能这样做。不过,您可以创建另一个变量并为其赋值:

unsigned short address2[] = { 0b10000001,0b01000010,0b00100100,0b00011000,
0b00011000,0b00100100,0b01000010,0b10000001 };
address = address2;

可能之后会对 sizeof(address) 产生问题;如果您需要,最好只使用另一个变量。

关于c - 如何在c中初始化数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251080/

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