【问题标题】:C exercise with malloc and realloc in a function在函数中使用 malloc 和 realloc 进行 C 练习
【发布时间】:2021-03-24 18:28:40
【问题描述】:

我有这个练习:

编写一个以三个值作为参数的 C 函数 RandArr2():

  • 整数数组 A
  • 一个整数 n,表示 A 的大小(元素数)
  • 整数 n'

函数RandArr2()返回一个数组,如下:

  • 如果 n == n',则返回 A
  • 如果 n > n',它会重新分配数组 A,创建一个新的整数数组 A',大小为 n'
  • 如果 n

这是我返回分段错误的实现。

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int * RandArr2(int * A, int n, int n1) {
  int i;
  if (n == n1) {
    for (i = 0; i < n; i++) {
      A[i] = rand();
    }
    return A;

  } else if (n < n1) {
    //reallocs the array A, creating a new array of integers A’ of size n’
    A1 = (int * ) realloc(A, n1 * sizeof(int));
    return A1;
  } else {
    /*reallocs the array A, creting a new array of integers A’ of size n’; it also fills all the
    positions of A’ between n and n’ with random integers between -10 and 10 */
    A1 = (int * ) realloc(A, n1 * sizeof(int));
    for (i = n; i < n1; i++) {
      A1[i] = -10 + rand() % (-10 - 10 + 1);
    }
    return A1;
  }
}

int main() {
  int n, n1;
  int * A;
  int i;
  printf("insert two integer:");
  scanf("%d, %d", & n, & n1);
  A = (int * ) malloc(n * sizeof(int)); //inizializzo l'array
  if (A != NULL) {
    A = RandArr2(A, n, n1);
    for (i = 0; i <= n; i++) {
      printf("\n Elemento del vettore: %d", A[i]);
    }
    free(A);
  } else {
    printf("Error!");
    exit(0);
  }
  return 0;
}

我是 C 新手,因此欢迎您提供各种帮助。

【问题讨论】:

  • 您的n&lt;n1n&gt;n1 案例混淆了。此外,您应该在调用RandArr2 之前填充A

标签: c pointers malloc realloc


【解决方案1】:

我已经修复了您程序中与分段错误相关的一些问题。我删除了rand() 函数以使事情更清楚。您可以根据自己的要求添加rand()

然而,主要问题是,在main()for 循环中,您正在打印返回数组的内容。我已经修好了。请检查main()for 循环中的test-expression 以了解该问题。数组的索引不能超过array_size-1,因为数组的索引从0开始。

固定代码如下。

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int* RandArr2(int* A, int n, int n1) {
    int i;
    int* A1 = NULL;
    if (n == n1) {
        for (i = 0; i < n; i++) {
            A[i] = 0xDEAD;
        }
        return A;

    }
    else if (n > n1) {
        //reallocs the array A, creating a new array of integers A’ of size n’
        A1 = (int*)realloc(A, n1 * sizeof(int));
        return A1;
    }
    else {
        /*reallocs the array A, creting a new array of integers A’ of size n’; it also fills all the
        positions of A’ between n and n’ with random integers between -10 and 10 */
        A1 = (int*)realloc(A, n1 * sizeof(int));
        for (i = 0; i < n; i++) {
            A[i] = 0xDEAD;
        }
        for (i = n; i < n1; i++) {
            A1[i] = 0xBEAD;
        }
        return A1;
    }
}
#define _CRT_SECURE_NO_WARNINGS
int main() {
    int n, n1;
    int* A;
    int i;
    printf("insert two integer:");
    scanf_s("%d, %d", &n, &n1);
    A = (int*)malloc(n * sizeof(int)); //inizializzo l'array
    if (A != NULL) {
        A = RandArr2(A, n, n1);
        for (i = 0; i < ((n > n1) ? n1 : n); i++) {
            printf("\n Elemento del vettore: 0x%x", A[i]);
        }
        free(A);
    }
    else {
        printf("Error!");
        exit(0);
    }
    return 0;
}

【讨论】:

  • 在你写的 print for 循环中 ((n > n1) ? n1 : n);这是因为我不知道我返回的数组的大小是否为 n 或 n1。它是正确的?我从来没有见过这个sintax
  • 这只是一个三元运算符。这里的想法是只运行 for 循环,直到分配数组的最后一个索引。
  • 完美,感谢您的回答。现在代码运行了,但我认为 realloc 不起作用:/
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2012-09-19
  • 2010-12-21
  • 1970-01-01
  • 2018-08-29
  • 2012-03-16
  • 1970-01-01
相关资源
最近更新 更多