我越来越想打印出一个字符数组用CGCC - 我应该如何正确格式化此字符串?分段错误
#include <stdio.h>
void printLoop(char args[])
{
char* p;
for (*p = *args; *p != '\0'; p++)
{
printf("%s", p);
}
getchar();
}
int main()
{
char *text = "Test";
printLoop(text);
}
我收到的时候分段错误:
Segmentation fault: 11
我读过它有什么与我分配给阵列的空间很大,但我不明白为什么我会得到这个。
将'printLoop'中的'for'更改为'for(p = args; * p!='\ 0'; p ++)'。祝你好运。 –
嘿,谢谢我设法在另一篇文章中弄明白,但现在我收到一个格式错误。它印刷“Testeststt”而不仅仅是“测试”。任何想法为什么? –
您得到“Testeststt”,因为您正在打印*以p *开始的字符串,而不是*由p *指向的字符。所以你打印“测试”,然后“est”,然后“st”,然后“t”。 – Beta