c - 以 int 指针 * 作为参数并返回结构体的函数。在struct struct*中存储数据并打印结果

标签 c struct

我遇到了这个我认为不应该发生的问题: 这是我收到的错误。 抱歉,格式错误,在记事本上编码,并 cntr c&v 到编译器(往往会不时这样做)

~/workspace/Exam1 $ gcc -g payload.c -o payload
payload.c: In function ‘main’:
payload.c:63:31: warning: initialization from incompatible pointer type [enabled by default]
     struct payload* calcVal = getCalc(arr, num);
                               ^
payload.c:65:36: error: dereferencing pointer to incomplete type
     printf("OddSum : %d\n", calcVal->sumOdd);
                                    ^
payload.c:66:40: error: dereferencing pointer to incomplete type
     printf("Even Count : %d\n", calcVal->evenCount);
                                        ^
payload.c:67:37: error: dereferencing pointer to incomplete type
     printf("Minimum : %d\n", calcVal->min);
                                     ^
payload.c:68:37: error: dereferencing pointer to incomplete type
     printf("Maximum : %d\n", calcVal->max);

老实说,我不明白问题是什么以及为什么我一直遇到这个问题。我传递结构错误吗?

#include<limits.h> //for min/max 
#include <stdio.h>
#include <stdlib.h>

struct payLoad{
    int sumOdd;
    int evenCount;
    int min;
    int max;
};

struct payLoad* getCalc(int *arr, int n){
    int i = 0;
    int min= INT_MAX;
    int max = INT_MIN;
    int sum = 0;
    int even = 0;
    struct payLoad* calcVal = malloc(sizeof(struct payLoad));  //watch spelling


    for(i=0;i <n;++i){
        if(arr[i]%2==0){
            ++even;
        }
        if (arr[i] < min){
            min = arr[i];
        }
    }

    if (arr[i] > max){
        max = arr[i];
    }
    if(arr[i]%2==1){
        sum = sum + arr[i];
    }

    calcVal->sumOdd = sum;
    calcVal->evenCount = even;
    calcVal->max = max;
    calcVal->min = min;

    return calcVal;

}

int main(){

    int num; 
    int i=0; 
    //int min= INT_MAX:
    //int max = INT_MIN: 
    int sum = 0;

    printf("Enter the number of array element: ");
    scanf("%d", &num);
    int *arr = (int *)malloc(num*sizeof(int));

    for(i=0;i <num;++i){
        printf("Enter the value: ");
        scanf("%d", &arr[i]);
    }

    struct payload* calcVal = getCalc(arr, num);

    printf("OddSum : %d\n", calcVal->sumOdd);
    printf("Even Count : %d\n", calcVal->evenCount);
    printf("Minimum : %d\n", calcVal->min);
    printf("Maximum : %d\n", calcVal->max);
}

编辑:修复了拼写错误和拼写,删除了链接,cntr c&v 错误

最佳答案

您的程序有几个拼写错误,例如您的几个地方:

struct payload
          ^ 

有效负载应为payLoad

这里:

if(arr[i]%2==1)
    sum = sum + arr[i];
}

您忘记输入{。应该是:

if(arr[i]%2==1) {
    sum = sum + arr[i];
}

main()中:

int num,

而不是应该是;

再次在main()中:

printf("OddSum : %d\n", calcVal->oddSum);

查看您的 struct payLoad 声明,您会发现它有一个成员 sumOdd 而不是 oddSum

编译器必须将所有这些报告为错误。仔细观察它们,尝试找出原因,修复它并重新构建。

无论您动态分配什么内存,都应该释放它。一旦程序终止,它所拥有的内存将被自动释放,但作为一种良好的编程实践,您应该在使用完毕后显式释放动态分配的内存。

关于c - 以 int 指针 * 作为参数并返回结构体的函数。在struct struct*中存储数据并打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48837381/

相关文章:

c - execv() 执行进程时,如何在 C 中杀死进程及其所有子进程?

arrays - swift : protocol extension and arrays

c - 确定 struct 中的无符号整数值是否已设置

c - 使用 termios() 替换 EOF?

c++ - 如何使用 libjpeg 将 YUYV 原始数据压缩为 JPEG?

c - 如何以编程方式将整数映射到 const 字符串?

c++ - openssl 函数定义密码顺序

json - 我如何在 Swift 中解码 JSON,它是一个包含双嵌套项目的数组?

C++ 结构大小不等于 C++/CLI 中的 C++ 结构大小?

c - 错误: dereferencing pointer to incomplete type in main file