【发布时间】:2017-04-18 04:42:16
【问题描述】:
编写一个程序,要求用户输入数组大小“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[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]);
}
【问题讨论】:
-
您的实际问题是什么?什么具体问题阻碍了你的工作?
-
如果
size具有动态值,则不能使用int arr1[size];声明静态数组。 tutorialspoint.com/cprogramming/c_memory_management.htm -
此外,
scanf_s("%d", arr1[size]);有多种原因是错误的。arr1[size]是int,而不是根据需要指向int的指针。无论如何它都超出了范围,因为size不是一个有效的索引。 -
请阅读您的教科书或笔记。任何基本的 C 书籍或教程都很好地介绍了内存分配。如果您要学习,请使用您已经可用的资源,并且只有在完成基础研究后才寻求进一步的帮助。
-
如果你真的找不到任何关于如何使用
malloc的解释或例子,那么你真的需要磨练你的谷歌fu技能。