用户输入数组大小C
编写一个程序,要求用户为数组大小“n”输入一个值,并用n个整数填充数组。然后反转阵列并在屏幕上打印。 我正在使用Visual Studio,到目前为止: “int arr1[size]”中的“size”有问题。它说它必须是一个常量用户输入数组大小C,c,arrays,C,Arrays,编写一个程序,要求用户为数组大小“n”输入一个值,并用n个整数填充数组。然后反转阵列并在屏幕上打印。 我正在使用Visual Studio,到目前为止: “int arr1[size]”中的“size”有问题。它说它必须是一个常量 #include <stdio.h> int main(void) { int size, i; printf("Enter the size of the arrays:\n"); scanf("%d", &size); int arr1[si
#include <stdio.h>
int main(void)
{
int size, i;
printf("Enter the size of the arrays:\n");
scanf("%d", &size);
int arr1[size];
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
scanf_s("%d", arr1[size]);
}
printf("The current array is:\n %d", arr1[i]);
}
#包括
内部主(空)
{
int大小,i;
printf(“输入数组的大小:\n”);
scanf(“%d”,大小(&S);
int arr1[大小];
printf(“输入数组的元素:\n”);
对于(i=0;i
在C语言中,不能以这种方式使用用户定义的大小定义数组
不正确的方式:
正确的方法:
不能使用静态内存分配来分配具有动态大小的数组。您需要动态地请求操作系统为您的程序分配内存,无论使用要求的动态大小如何 下面是一个开始的示例:
#include <stdlib.h>
#include <stdio.h>
void printArray(int *array, int size) {
// Sample array: [1, 2, 3, 4, 5]
printf("["); // [
for (int i = 0; i < size - 1; i++) { // [1, 2, 3, 4,
printf("%i, ", array[i]);
}
if (size >= 1) printf("%i", array[size-1]); // [1, 2, 3, 4, 5
printf("]\n"); // [1, 2, 3, 4, 5]
}
int main(void) {
int count;
printf("Enter the size of the array:\n");
scanf("%d", &count);
// ask for enough memory to fit `count` elements,
// each having the size of an `int`
int *array = malloc(count * sizeof(*array));
if (!array) {
printf("There was a problem with malloc.");
exit(EXIT_FAILURE);
}
printf("Enter the elements of the array:\n");
for (int i = 0; i < count; i++) scanf("%d", &array[i]);
printArray(array, count);
//do stuff
free(array);
}
#包括
#包括
无效打印数组(整数*数组,整数大小){
//样本数组:[1,2,3,4,5]
printf(“[”);//[
对于(inti=0;i=1)printf(“%i”,数组[size-1]);//[1,2,3,4,5
printf(“]\n”);//[1,2,3,4,5]
}
内部主(空){
整数计数;
printf(“输入数组的大小:\n”);
scanf(“%d”、&count);
//要求有足够的内存来容纳'count'元素,
//每一个都有一个int大小`
int*array=malloc(count*sizeof(*array));
if(!数组){
printf(“malloc有问题”);
退出(退出失败);
}
printf(“输入数组的元素:\n”);
对于(int i=0;i
C的一些方言有(VLA),特别是(和接受…)
但是你的老师可能想让你理解,你也应该在什么时候测试,这样你就可以编写代码:
int main(void) {
int size, i;
printf("Enter the size of the arrays:\n");
if (scanf("%d", &size)<1) {
perror("scan size");
exit(EXIT_FAILURE);
};
您始终需要处理calloc
或malloc
printf(“输入数组的元素:\n”);
对于(i=0;i 如果(size
具有动态值,则不能使用int-arr1[size];
来声明静态数组。此外,scanf's(%d),arr1[size];
由于多种原因错误。arr1[size]
是一个int
而不是根据需要指向int
的指针。而且它是超出范围的,因为大小
不是一个有效的索引。请阅读您的教科书或笔记。内存分配在任何基本的C语言书或教程中都有详细介绍。如果您要学习,请使用您已经可以使用的资源,并且只使用se一旦你完成了基础研究,ek将进一步帮助你。如果你真的找不到任何关于如何使用malloc
的解释或例子,那么你真的需要磨练你的google fu技能。一些C语言已经有了,特别是C99(并接受……)
#include <stdlib.h>
#include <stdio.h>
void printArray(int *array, int size) {
// Sample array: [1, 2, 3, 4, 5]
printf("["); // [
for (int i = 0; i < size - 1; i++) { // [1, 2, 3, 4,
printf("%i, ", array[i]);
}
if (size >= 1) printf("%i", array[size-1]); // [1, 2, 3, 4, 5
printf("]\n"); // [1, 2, 3, 4, 5]
}
int main(void) {
int count;
printf("Enter the size of the array:\n");
scanf("%d", &count);
// ask for enough memory to fit `count` elements,
// each having the size of an `int`
int *array = malloc(count * sizeof(*array));
if (!array) {
printf("There was a problem with malloc.");
exit(EXIT_FAILURE);
}
printf("Enter the elements of the array:\n");
for (int i = 0; i < count; i++) scanf("%d", &array[i]);
printArray(array, count);
//do stuff
free(array);
}
int main(void) {
int size, i;
printf("Enter the size of the arrays:\n");
if (scanf("%d", &size)<1) {
perror("scan size");
exit(EXIT_FAILURE);
};
int *arr1 = calloc(size, sizeof(int));
if (arr1==NULL) {
perror("calloc arr1");
exit(EXIT_FAILURE);
}
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
if(scanf("%d", &arr1[size])<1) {
perror("scanf arr1[size]");
exit(EXIT_FAILURE);
}
}