T. Decker
T. Decker

Reputation: 145

How would you read these definitions?

How should I read each of these definitions ?

  1. const char *arguments[]
  2. char *const arguments[]

I saw examples of execl() code using the first form but could not make it work under Linux and had to use the second form ?

Upvotes: 3

Views: 103

Answers (2)

KamilCuk
KamilCuk

Reputation: 141165

const char *arguments[]

arguments is an array of unknown size of pointers to const qualified char.

char *const arguments[]

arguments is an array of unknown size of const qualified pointers to char.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311028

In C declarators are defined the following way

declarator:
    pointeropt direct-declarator

where the pointer is defined like

pointer:
    * type-qualifier-listopt
    * type-qualifier-listopt pointer

So this declaration

char *const arguments[]

can be rewritten like

char ( * const ( arguments[] ) )

So there is declared an array of unknown size of constant pointers to char. That is you may not change the elements of the array because they are constant. But you may change the objects pointed to by the elements of the array because the pointed objects are not themselves constant.

Used as a parameter declaration this declaration is implicitly adjusted by the compiler to this declaration

char ( * const * arguments )

For example these two function declarations declare the same one function

void f( char ( * const ( arguments[] ) ) );
void f( char ( * const * arguments ) );

This declaration

const char *arguments[]

declares an array of unknown size of non-constant pointers to const char. That is you may change the elements of the array but you may not change the objects pointed to by the elements of the array beacuse the pointed objects themsekves are constant.

This declaration may be rewritten like

const char ( * ( arguments[] ) )

Or it is adjusted by the compiler to the declaration

const char ( **  arguments )

These two function declarations

void f( const char ( * ( arguments[] ) ) );
void f( const char ( ** arguments ) );

declare the same one function.

Upvotes: 1

Related Questions