Home > front end >  What is the right way to pass a pointer to my function?
What is the right way to pass a pointer to my function?

Time:09-22

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?

CodePudding user response:

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);
  • Related