我有一些小烦恼而不是错误,但我无法弄清楚它为什么会发生。我有一个数组索引和 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);
}
}