char letM[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int nr;
printf("enter a number between 7 and 15\n");
scanf("%d", &nr);
for (int j = 0; j<=nr-5; j++){
char letter[j] = letM[rand()%26+1];
printf("%c", letter);
}
此代码应为图章nr-5字母,但在我运行它时,输出会显示错误:可变大小的对象可能未初始化
char letter[j];
definesletter
to be an array containingj
elements, each of which is achar
. Becausej
is a variable, this is called a variable length array.char letter[j] = letM[rand()%26+1];
defines such an array and attempts to initialize it with the value ofletM[rand()%26+1];
. The C standard does not provide for initializing variable length arrays. (They must be given values via ordinary assignments statements or other means, not via initializers in declarations.)You may have meant to declare
letter
to be a singlechar
. In this case, change the code tochar letter = letM[rand()%26+1];
.You have to declare what
letter
is before using it. Here's an option if you want to store the letters. Besides, when you printletter
don't forget to printletter[j]
instead.否则,就像其他建议一样,在这里,如果您只想打印字母,则甚至不需要变量: