我有一个长数组,我想弄清楚如何将它分成两个独立的数组,第二个数组有正确的内容,但第一个是空的,我也得到一个Abort Trap:6和我'我不知道这意味着什么。
我有一个名为wholeA的数组,它看起来像这样:
你好:世界,我想把HELLO放在一个单独的阵列(firstA)和WORLD在secondA。当我在末尾打印第一个和第二个数组时,secondA有正确的内容,但是第一个A没有事件,虽然我正在打印以检查是否正在传递正确的字符并且它们是 - 但是firstA仍然是空的而我我得到并中止陷阱我不明白。
我刚刚开始学习C,为什么第一个数组为空,错误是什么意思?
#define ARRSIZE 10000
char entireA[ARRSIZE] = "";
char firstA[ARRSIZE] = "";
char secondA[ARRSIZE] = "";
strcpy(entireA,"HELLO:WORLD\n");
int firstVar = 0;
int entireVar = 0;
while(entireA[entireVar] != ':') {
if(entireA[entireVar] == ';') {
break;
}
printf("%c \n",entireA[entireVar]);
firstA[firstVar] = entireA[entireVar];
firstVar++;
entireVar++;
}
firstA[firstVar] = '\0';
int secondVar = 0;
entireVar++; //skip ':'
while(entireA[entireVar] != '\n') {
secondA[secondVar] = entireA[entireVar];
secondVar++;
entireVar++;
}
secondA[secondVar] = '\0';
printf("%s", firstA);
printf("%s", secondA);
您发布的代码没有任何问题。
执行后,变量具有以下值:
entireA 0x02efcdb4 "HELLO:WORLD\n" char[0x00002710]
entireVar 0x0000000b int
firstA 0x02efa69c "HELLO" char[0x00002710]
firstVar 0x00000005 int
secondA 0x02ef7f84 "WORLD" char[0x00002710]
secondVar 0x00000005 int
无论您遇到什么问题,最有可能与您的环境有关。我建议将ARRSIZE的值减少到80个字符,并查看是否会改变您的结果。