我有一些小烦恼,而不是错误,但我不明白为什么会发生这种情况。我有一个数组索引和 2D gchar 数组声明如下:
gchar FilterArray[10][51];
static uint8_t ArrayIndex = 0;
static gchar ScanLine[9640];
现在,我第一次使用数组索引时,它的值不知何故为 115。在使用它之前,我没有在代码中的任何位置设置它。如果我只在 strcpy 命令中使用它之后打印它,则它的值为 115。如果我在 strcpy 命令之前打印它,则它的值在程序运行期间是正确的。
while(FilterAmount != 0)
{
g_io_channel_read_chars (source,(gchar *) ScanLine,1,&BytesRead,&GlibError);
if(ScanLine[0] == FilterTerminator[0]) {
printf("Array Index: %i\n", ArrayIndex);
if(strlen(FilterName) > 0){
printf("Array Index: %i\n", ArrayIndex); //if I only print before, value is correct
strcpy(FilterArray[ArrayIndex],FilterName);
printf("Array Index: %i\n", ArrayIndex); //if I only print after, value is incorrect
ArrayIndex++;
FilterAmount--;
FilterName[0] = '\0';
}
}
else {
strcat(FilterName, ScanLine);
}
}
最佳答案
据推测,FilterName
太长,并且 strcpy(FilterArray[ArrayIndex],FilterName)
写入超出 FilterArray
末尾并超过 数组索引
。如果在 strcpy
之前打印,ArrayIndex
似乎是正确的,可能是因为该值保存在寄存器中,并且不会从覆盖的内存位置再次加载。
关于c - Uint8_t 设置自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508781/