3

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 ?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
T. Decker
  • 137
  • 6
  • 2
    Use [this](https://cdecl.org/) to figure out what such declarations mean. – Blaze Sep 27 '19 at 10:26
  • @Blaze: According to that, `char *const arguments[]` means `Error error`. – 500 - Internal Server Error Sep 27 '19 at 10:31
  • 3
    It's also worth reading about [the spiral rule](http://c-faq.com/decl/spiral.anderson.html) – byxor Sep 27 '19 at 10:32
  • (1) gives me: declare arguments as array of pointer to const char (2) gives me: declare arguments as array of const pointer to char. – T. Decker Sep 27 '19 at 10:36
  • @T.Decker And there you have the answer. – klutt Sep 27 '19 at 10:37
  • 1
    What do you mean with "could not make it work under Linux"? If you get an error message, please show it in your question and also show the code that produces the error. Function `execl()` doesn't take an array, but function `execv()` does and this is declared as `int execv(const char *path, char *const argv[]);`, so you have to use the 2nd form. – Bodo Sep 27 '19 at 11:18

2 Answers2

2

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335