【问题标题】:User input array size C [duplicate]用户输入数组大小 C [重复]
【发布时间】: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技能。

标签: c arrays


【解决方案1】:

C 的一些方言有variable-length arrays (VLA),尤其是C99(并且GCC 接受them....)

但你的老师可能希望你理解C dynamic memory allocation,你也应该测试scanf fails,所以你可能会编码:

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);
    };

其实你最好针对size是否定的情况进行测试!我把它留给你。

    int *arr1 = calloc(size, sizeof(int));
    if (arr1==NULL) {
       perror("calloc arr1");
       exit(EXIT_FAILURE);
    }

你总是需要处理callocmalloc的失败

 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);
    }
 }

scanf_s is 仅存在于 C11,在符合 C11 的实现中,您可以使用 VLAs

剩下的交给你。你想编写一个for 循环来打印数组的每个元素。

当然你应该打电话给free appropriately(我把这个留给你)。否则,你有一个memory leak。一些系统有valgrind 来帮助寻找它们。不要忘记编译所有警告和调试信息(GCC 使用 gcc -Wall -g)然后使用调试器(例如gdb)。

不要忘记编程语言是规范(写在某些文档中),而不是软件。你可能想看看n1570。而且您需要了解undefined behavior 的含义。

【讨论】:

    【解决方案2】:

    您不能使用静态内存分配来分配具有动态大小的数组。您需要动态地请求内存以由操作系统分配给您的程序,无论用户需要什么动态大小。

    这是一个开始的例子:

    #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);
    }
    

    【讨论】:

      【解决方案3】:

      在 C 中,不能以这种方式使用用户定义的大小来定义数组。

      方法不对:

      int size, i;
      printf("Enter the size of the arrays:\n");
      scanf("%d", &size);   
      int arr1[size];
      

      正确方法:

      int size ;
      int *arr1 ;
      printf("Enter the size of the arrays:\n");
      scanf("%d", &size);  
       arr1 = (int *) malloc(sizeof(int) * size);
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多