简体   繁体   English

如何在函数中传递参数数组指针?

[英]How to pass argument array pointer in function?

I have this code: 我有以下代码:

// *** foo.c ***

#include <stdlib.h> // malloc

typedef struct Node {
    char (*f)[20];
    //...
} Node;

int function(char f[30][20]){
    // Do some stuff with f...
}

int main(){
    Node * temp = (Node*)malloc(sizeof(Node));
    function(temp->f[20]);
}

I would like to pass temp->f array pointer to function, but on compile I get these errors: 我想将temp->f数组指针传递给函数,但是在编译时出现以下错误:

$ gcc foo.c -o foo
foo.c: In function ‘main’:
foo.c:16:5: warning: passing argument 1 of ‘function’ from incompatible pointer type [enabled by default]
     function(temp->f[20]);
     ^
foo.c:10:5: note: expected ‘char (*)[20]’ but argument is of type ‘char *’
    int function(char f[30][20]){
        ^

How can I fix it? 我该如何解决?

expected 'char (*)[20]' but argument is of type 'char *

Change the way you call the function to: 将调用函数的方式更改为:

function(temp->f);

Also: 也:

When you pass an array "f" as argument, you are passing its first item ADDRESS (as arrays are pointers) But when you pass f[20] as argument, you are passing the VALUE of the 20th element of f 当您将数组“ f”作为参数传递时,您将传递其第一项ADDRESS(因为数组是指针),但是当您将f [20]作为参数传递时,您将传递f的第20个元素的值

So, if you pass (f) (or temp->f in your case) you are passing an address and the destination shall be "char*" type to receive. 因此,如果您传递(f)(或者您的情况是temp-> f),则您传递的是地址,并且目的地应为“ char *”类型以进行接收。

And if you pass f[20] (or temp->f[20]) you are passing a value and the destination shall be "char" type 并且如果您传递f [20](或temp-> f [20]),则您传递的是值,并且目的地应为“ char”类型

If you want to receive the address of the 20th item, you should pass &(f[20]) ( or &(temp->f[20]) ) 如果要接收第20个项目的地址,则应传递&(f [20])(或&(temp-> f [20]))

Just to clarify: "f" is equivalent to "&(f[0])", as both represents the address of the first item 需要澄清的是:“ f”等效于“&(f [0])”,因为两者都代表第一项的地址

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

相关问题 如何在ctypes中生成字符数组并将其指针作为函数参数传递 - How to produce a character array in ctypes and pass its pointer as a function argument 使用指针参数将固定大小的数组传递给函数 - Pass fixed sized array to a function with pointer argument 如何修改并将数组指针作为参数传递给需要数组作为参数的函数? - How to modify and pass array pointer as argument to a function which require array as argument? 如何在函数中传递“指向数组的指针” - how to pass “pointer to array” in function 如何将数组指针传递给函数? - How to pass an array pointer to a function? 指向字符数组的指针并传递给二维字符数组 function 参数 - Pointer to character array and pass to 2d character array function argument 如何在C中的可变参数函数中将结构指针作为参数传递 - how to pass struct pointer as argument in a variable argument function in C 如何使用包装函数传入函数的参数指针函数? - How to pass in function's argument a pointer function using wrapper function? 关于如何使用指针将数组作为函数参数传递的代码片段的说明? - Explanation of code snippet regarding how to pass an array as a function argument using a pointer? 如何将“指针指向指针”传递给期望“指针指向数组”的函数 - How to pass “pointer to a pointer” to a function that expects “pointer to array”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM