简体   繁体   English

为什么除main以外的函数中的数组初始化是临时的?

[英]why does array initialization in function other than main is temporary?

This is the code in which i initialize array "turn[20]" in main as well as in function "checkCollisionOrFood()",the four values turn[0],turn[1],turn[2],turn[3] are initialized to zero in main function,rest are being intialized in "checkCollisionOrFood()".This is where fault starts.when i initialize turn[4]=0 in "checkCollisionOrFood()" and then access it anywhere,it remains 0 in any function,but! 这是我在main和函数“ checkCollisionOrFood()”中初始化数组“ turn [20]”的代码,四个值分别为turn [0],turn [1],turn [2],turn [3]在主函数中初始化为零,其余部分在“ checkCollisionOrFood()”中初始化。这是故障开始的地方。当我在“ checkCollisionOrFood()”中初始化turn [4] = 0并在任何地方访问它时,它保持为0。任何功能,但是! when i initialize next turn[] ie turn[5],the value of turn[4] gets depleted .ie turn[4] have garbage value.turn[20] is global variable,its index"head" is also global.I'm stuck with this problem plz help me get out of it.This is my complete code 当我初始化下一个turn []即turn [5]时,turn [4]的值将耗尽。即turn [4]具有垃圾值。turn[20]是全局变量,其索引“ head”也是全局的。卡住了这个问题,请帮助我摆脱困境。这是我完整的代码

#include <mega8.h>
#include <delay.h>
#include <stdlib.h>

unsigned short address[]=    {0b00001111,0b00000000,0b00000000,0b00000000,
  0b00000000,0b00000000,0b00000000,0b00000000};
unsigned short turn[20];//it is declared as global variable.
unsigned short head=3;
unsigned short colHead=0;
unsigned short rowHead =3;
unsigned short rowTail=0;
unsigned short colTail=0;
unsigned short size=4;
unsigned short foodx;
unsigned short foody;

...

unsigned short checkCollisionOrFood()
{
head=(head+1)%20;
if(turn[head-1]==0){
    turn[head]=0;

/ this is where turn[] is iniliazized and if i access turn[head] here ie just after iniliazition then it gives correct value but if i access its previous value means turn[head-1]then it gives garbage value / / 这是turn []被初始化的地方,如果我在此处访问turn [head],即在初始化之后,它会给出正确的值,但是如果我访问其先前的值意味着turn [head-1],那么它将给出垃圾值 /

    PORTB=255;PORTB&=~(1<<turn[4]);PORTD.7=0;delay_ms(200);PORTD.7=1;

/ by this statement i checked the value of turn[4] just after initilization of turn[4] which gives right answer and then after initialization of turn[5] and accessing turn[4] gives garbage. / 通过此语句,我在turn [4]初始化后立即检查了turn [4]的值,该值给出了正确的答案,然后在turn [5]初始化并访问turn [4]后产生了垃圾。 / /

    rowHead=(rowHead+1)%8;
    if(!(address[colHead]&(1<<rowHead)))return 1;
    else if((address[colHead]&(1<<rowHead))&&
    (!((colHead==foody)&&(rowHead==foodx))))gameOver();
    else return 0;
    }
if(turn[head-1]==1){
    turn[head]=1;
    colHead=(colHead+1)%8;
    if(!(address[colHead]&(1<<rowHead)))return 1;
    else if((address[colHead]&(1<<rowHead))&&
    (!((colHead==foody)&&(rowHead==foodx))))gameOver();
    else return 0;
    }
}
void main(void)
{
turn[0]=0;turn[1]=0;turn[2]=0;turn[3]=0;

/ these values of turn[] are not changed irrespective of where they are accessed. / 不论在何处访问,turn []的这些值都不会更改。 / /

while (1)
  {
  if(checkCollisionOrFood())
    {
    PORTB=(address[colHead] |=1<<rowHead);
    turnOffTail();
    blink();
    }
  else
    {
    PORTB=address[colHead];
    createFood();
    blink();
    }
  }

} }

Plz suggest me to get out of it. Plz建议我摆脱它。

If you observer garbage in head this could easily due to accessing address out of bounds, as this is located in memrory directly before turn 如果你观察垃圾head这很容易由于访问address出界,因为这个位于memrory直接前turn

Check the indexing of address to make sure you do not write beyond it's boundaries and with this overwrite data in the next variable, that is turn . 检查address的索引,以确保您不会写超出其范围的内容,并使用下一个变量turn中的此数据进行覆盖。

unsigned short checkCollisionOrFood()
{
    head=(head+1)%20;
    if(turn[head-1]==0){
        turn[head]=0;

You might have a problem when "head" is reaching "20". 当“ head”达到“ 20”时,您可能会遇到问题。 The value of "head" is then rolling back to 0, which leads you to test the value of "turn[-1]". 然后,“ head”的值回滚为0,这将导致您测试“ turn [-1]”的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在声明以外的时间初始化 C 数组? - Initialization of C array at time other than declaration? 为什么gcc允许char数组初始化的字符串文字大于array? - Why does gcc allow char array initialization with string literal larger than array? 主函数以外的函数内部无法识别的结构 - structure not recognized inside a function other than main 从 function 在除 main 之外的其他文件中返回指向字符数组的指针 - returning pointer to a character array from a function in a different file other than main 为什么我这个地址在主 function 初始化后被更改? (C) - Why I this address is being changed after initialization in main function? (C) 从堆栈的角度看,为什么主函数与其他函数不同 - Why does main function differ from other function from stack point of view 如何在C中使除main()以外的其他函数作为入口点 - How to make other function as entry point other than main() in C 为什么溢出数组初始化会发出警告但溢出的赋值不会? - Why does overflowing array initialization give warning but overflowing assignment does not? 为什么声明main作为数组编译? - Why does declaring main as an array compile? 为什么它返回一个随机值而不是我给 function 的值? - Why does it return a random value other than the value I give to the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM