gpt4 book ai didi

c - 为什么 for 循环只运行了 4 次就终止了?

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:43 25 4
gpt4 key购买 nike

我希望随着用户的每次输入,数组(标记)大小增加 1:

#include <stdio.h>
main() {
int size=1;
int marks[size];
int i;
printf("Enter marks\n");
for(i=0;i<size;i++) {
scanf("%d",&marks[i]);
size++;
}
}

最佳答案

您不能动态增加数组的大小。但是,如果您知道将输入多少分数,则可以将大小设置为该值,然后接受值直到达到该大小。

#include <stdio.h>
int main()
{
int size = 10;
int marks[size];
int i;
printf("Enter marks\n");
for(i = 0; i < size; i++)
{
scanf("%d",&marks[i]);
}
}

动态调整大小会显着增加程序的复杂性,但如果这正是您想要做的,请在此处查看 R Sahu 的回答或此回答:increasing array size dynamically in c .

关于c - 为什么 for 循环只运行了 4 次就终止了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51772300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com