我编写了一个代码,用于查找数组的最小和最大元素并将其反转元素。但是在我构建它之后它给了我一个输出错误

I wrote a code that is finding a min and max elements of array and reverse it elements . But after I build it it gave me an output errors

我编写了这段与数组一起使用的代码,它分为两个文件,一个包含函数,另一个包含 main。但是当我编译它时,它给了我一个输出错误如下。你对我如何解决这个问题有什么建议吗?

functions.c:

#include <string.h> 
#include <stdio.h>

void return_array(int a[], int n) {
    int c, d, min, max, location = 1;
    int b[n];
    
    for (c = n - 1, d = 0; c >= 0; c--, d++)
        b[d] = a[c];
    
    for (c = 0; c < n; c++) {
        a[c] = b[c];
        
    }
    printf("Array after reverse of elements:\n");
    
    for (c = 0; c < n; c++) {
        printf("%d\n", a[c]);
    }
    return;
}

void return_min_max(int a[], int n, int * min, int * max) {
    for (int i = 1; i < n; i++) {
        if (a[i] > * max) {
            * max = a[i];
        }
        
        if (a[i] < * min) {
            * min = a[i];
        }
    }
    printf("Smallest element in array is: %d\n", * min);
    printf("Largest element in array is: %d\n", * max);
    return;
}

错误:

       6: error: multiple definition of `return_array'
       29: error: multiple definition of `return_min_max'
      :-1: error: collect2.exe: error: ld returned 1 exit status

首先,您发布的内容有几个问题 you shouldn't include .c files。相反,创建一个模块,将定义放在头文件中,并将实现放在 .c 文件中。

现在,在您的代码中,main() 函数的第一行中有 int a[n]。您希望 n 具有什么价值? n 未初始化,因此 n 的值是 indeterminate,这意味着您无法知道该值是什么。在你的情况下,我有一个段错误而运行这个,但在不同的计算机上结果可能不同。

这应该是您要查找的内容:

functions.h:

#ifndef FUNCTIONS_H /* include guard */
#define FUNCTIONS_H

void return_array(int a[], int n);

void return_min_max(int a[], int n, int *min, int *max);

#endif

functions.c:

#include "functions.h" /* include your header with the declarations */
#include <stdio.h>

void return_array(int a[], int n)
{
    int c, d;
    int b[n];

    for (c = n - 1, d = 0; c >= 0; c--, d++)
    {
        b[d] = a[c];
    }

    for (c = 0; c < n; c++)
    {
        a[c] = b[c];
    }
    printf("Array after reverse of elements:\n");

    for (c = 0; c < n; c++)
    {
        printf("%d\n", a[c]);
    }
    return;
}

void return_min_max(int a[], int n, int *min, int *max)
{
    for (int i = 1; i < n; i++)
    {
        if (a[i] > *max)
        {
            *max = a[i];
        }

        if (a[i] < *min)
        {
            *min = a[i];
        }
    }
    printf("Smallest element in array is: %d\n", *min);
    printf("Largest element in array is: %d\n", *max);

    return;
}

main.c:

#include "functions.h" /* note the double "" */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, c, min, max;

    printf("Enter number of elements to array\n");
    if (scanf("%d", &n) != 1 || n <= 0) /* check return value of scanf() for input errors */
    {                                   /* also make sure n is greater than 0 */
        exit(EXIT_FAILURE);
    }

    int a[n]; /* declare your VLA */

    printf("Enter elements to array\n");

    for (c = 0; c < n; c++)
    {
        if(scanf("%d", &a[c]) != 1)
        {
            exit(EXIT_FAILURE);
        }
    }

    min = a[0];
    max = a[0];

    return_min_max(a, n, &min, &max);
    printf("\n");

    return_array(a, n);

    return 0;
}

编译:

gcc -Wall -pedantic main.c functions.c