【发布时间】:2017-11-02 19:57:07
【问题描述】:
那是代码:
int qtd_alunos, qtd_temas;
char* tnome[1][10];
char* anome[1][10];
printf("\nPra distribuir os temas me diga quantos alunos vao participar e aperte enter, depois a quantidade de temas.");
scanf("%d%d", &qtd_alunos, &qtd_temas);
printf("Agora vai escrevendo o nome de cada tema\n");
for(int j = 0; j<qtd_temas; j++){
printf("Tema %d\n", j+1);
scanf("%s", tnome[0][j]);
printf("%s ok!\n",tnome[0][j]); }
return 0; }
当我运行代码时,程序只保存位置"tnome[0][0]",但当"int j" 更改为[0][1] 时,程序关闭。我需要了解为什么会这样。
【问题讨论】:
-
在询问运行时问题时,就像这个问题一样,发布minimal reproducible example
-
在调用任何
scanf()系列函数时: 1) 始终检查返回值(而不是参数值)以确保操作成功。 2) 当使用 '%s' 或 '%[...]' 输入转换说明符时,始终包含比输入字段长度小 1 的 MAX CHARACTERS 修饰符。少一个,因为scanf()总是为这些说明符附加一个 NUL 字节。
标签: c arrays string for-loop codeblocks