Floyd
Floyd

Reputation: 164

What is the meaning of int *(*papi[10])

I used cdecl and get its definition as "declare papi as array 10 of pointer to pointer to int" So I write my code in this way

int i = 10;
int *api[10];
api[0] = &i;
int *(*papi[10]);
papi = &api;

And I got an error says "array type 'int ([10])' is not assignable"

What is the correct way to use the papi?

Upvotes: 0

Views: 145

Answers (3)

haccks
haccks

Reputation: 106012

Array names are non-modifiable l-values. You can't use them as left operand of = operator. papi is an array name. Change

int *(*papi[10]);  

to

int *(*papi)[10]; // papi is a pointer to an array of 10 pointers to int

Upvotes: 4

Vlad from Moscow
Vlad from Moscow

Reputation: 310970

I interpretated phrase "declare papi as array 10 of pointer to pointer to int" as declare an array of 10 pointers to pointer to int.:)

Try the following

#include < stdio.h>


int main( void )
{
    int i = 10;
    int *api[10] = { &i };
    int **papi[10];
    papi[0] = api;

    printf( "%d\n", ***papi );
}

The output is

10

Upvotes: 1

mafso
mafso

Reputation: 5543

You cannot assign to an array in C. What you seem to want to do was:

int i = 10;
int *api[10];
api[0] = &i;
int *(*papi)[10];
papi = &api;

which declares papi as a pointer to an array of 10 pointers to int. This is the type of &api, what takes the address of a 10-element array of pointer to int.

HTH

Upvotes: 2

Related Questions