简体   繁体   English

C结构函数语法-传递元素,返回结构

[英]C Struct Function Syntax - Pass elements, return struct

I'm trying to create a function that accepts an array and two integers to manipulate and return in a struct. 我正在尝试创建一个函数,该函数接受一个数组和两个整数以进行操作并在结构中返回。

What i have looks like this: 我有什么看起来像这样:

#include <stdio.h>

struct Results {
    int *A; // Pointer para o Array
    int N; // Comprimento do Array
};

int k, n;
struct Results solution(int A[], int N, int K);

int main(void){
    int a[] = {1,2,3};
    struct Results out;

    k = 1;
    n = sizeof(a)/sizeof(a[0]);
    printf("n = %d \n", n);

    out = solution(int a[], int n, int k);
    // EXPECTED EXPRESSION !!
}

struct Results solution(int A[], int N, int K) {
    struct Results outp;
    outp.A = A;
    outp.N = N;
    return outp;
};

I can't pass from this point, the compiler tells me that a expression is expected when I declare the function. 从这一点上讲我无法通过,编译器告诉我在声明函数时需要一个表达式。

I think this might be a basic syntax error... 我认为这可能是基本的语法错误...

You are mixing the syntax for defining or declaring a function with the actual call to that function: 您正在将定义或声明函数的语法与对该函数的实际调用混合在一起:

out = solution(a, n, k);

The types of the parameters are only present in the prototype, not in the call. 参数的类型仅存在于原型中,而不存在于调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM