【问题标题】: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我编写了一个代码,它查找数组的最小和最大元素并将其反转元素。但是在我构建它之后它给了我一个输出错误
【发布时间】:2021-05-19 19:50:40
【问题描述】:

我编写了这个与数组一起使用的代码,它分为两个文件,一个包含函数,另一个包含 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

【问题讨论】:

  • 你如何编译
  • 如何将函数包含到 main 中?
  • @Armil 我在 qt creator 中使用 MinGW 7.3.0
  • @mugiseyebrows 我将我的主要内容发布到答案中。

标签: c function compiler-errors


【解决方案1】:

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

现在,在您的代码中,int a[n] 位于 main() 函数的第一行。你期望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;
}

ma​​in.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

【讨论】:

  • 非常感谢您的回答,这解决了我的问题:)
  • 很高兴我能帮上忙;)
猜你喜欢
  • 2020-03-26
  • 2020-02-09
  • 2023-01-30
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2014-08-28
  • 1970-01-01
相关资源
最近更新 更多