简体   繁体   中英

What is the right way to pass a pointer to my function?

I've a function prototype:

int array_length(char *(ptr)[1024]){
...
}

I want to call this function into another function. I have:

char array_slave[128][1024];
char (*ptr)[1024] = array_slave;
array_length(&ptr);

As the compiler says, this is wrong. But why? Can you explain me "theorically" how to do in this situation? What is the reasoning to do?

The type of ptr is already char(*)[1024] . Thus the type of &ptr is a pointer to pointer to an array char(**)[1024] . It differs from what the function expects thus a warning is raised. Just skip & :

array_length(ptr);

Alternatively you can pass array_slave directly because an array of char[1024] decays to a pointer to its first element producing pointer of char(*)[1024] type.

array_length(array_slave);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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