-1

What is the difference between these two declarations ? How are such variables used in C programs ? Thank you.


char *names[20];

[] takes precedence over *, so the interpretation is: names is an array of size 20. Each array element is of type pointer to char.


char (*place)[10];

In case of using parentheses to override the precedence, the * is applied first: place is a pointer to an array of size 10. Each array element is of type char.

Mina Karam
  • 124
  • 1
  • 1
  • 10

2 Answers2

1

Clockwise/Spiral Rule


char *names[20];

Applying the rule, we learn that names is a 20-element array of pointers to char values.

More clearly, names is an array of 20 char * pointers. This allocates enough memory for 20 such pointers.

sizeof(names) == sizeof(char*) * 20

char (*place)[10];

Applying the rule, we learn that place is a pointer to a 10-element array of char values.

More clearly, place is a pointer, and it points to an array of 10 char. This allocates enough memory for one such pointer.

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

This declaration

char *names[20];

declares an array with the name names of 20 elements of the type char *. It would be better to write it like

char * names[20];

This declaration

char (*place)[10];

declares a pointer with the name place that points to an array of 10 elements of the type char.

Here is a demonstrative program that I hope makes it more clear.

#include <stdio.h>

int main(void) 
{
    char * names[20] =
    {
        "Peter", "Bob", "Mary"
    };
    
    for ( char **p = names; *p != NULL; ++p )
    {
        printf( "%s ", *p );
    }
    
    putchar( '\n' );
    
    char city[10] = "Madrid";
    
    char (*place)[10] = &city;
    
    puts( *place );
    
    return 0;
}

The program output is

Peter Bob Mary 
Madrid

In this declaration in the above program

char * names[20] =
{
    "Peter", "Bob", "Mary"
};

three elements of the array are initialized explicitly by addresses of first characters of the string literals used as initializers. All other 17 elements of the array are implicitly initialized as null pointers.

The same program can be rewritten using typedef(s).

#include <stdio.h>

typedef char * T1;
typedef char T2[10];

int main(void) 
{
    T1 names[20] =
    {
        "Peter", "Bob", "Mary"
    };
    
    for ( char **p = names; *p != NULL; ++p )
    {
        printf( "%s ", *p );
    }
    
    putchar( '\n' );
    
    T2 city = "Madrid";
    
    T2 *place = &city;
    
    puts( *place );
    
    return 0;
}

The program output will be the same as shown for the previous program.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    `"It would be better to write it like char * names[20];"` -- I disagree with this statement, because if you write `char * names[20], cities[20];`, the position of the `*` will be misleading, implying that both `names` and `cities` were of the same type. – Andreas Wenzel May 12 '21 at 16:34
  • @Andreas Wenzel, The statement you posted isn't the statement Vlad recommended. Only your statement is misleading. To that I say, so don't do that! – ikegami May 12 '21 at 16:39
  • @ikegami: My point is that, in my opinion, it is a bad habit to write it that way, because if you later decide to add a second identifier to the declaration, you can easily forget to add a `*` to the second identifier. The danger of this happening is significantly reduced if you write it the "normal" way. – Andreas Wenzel May 12 '21 at 16:44
  • @AndreasWenzel It is just your personal problem that you write a confusing code. It has nothing common with the code I showed. Try to write a clear code. – Vlad from Moscow May 12 '21 at 16:45
  • @Andreas Wenzel, Re "*because if you later decide to add a second identifier to the declaration*", And my point is that this is a problem of your own doing. Just don't do that. /// Re "*The danger of this happening is significantly reduced if you write it the "normal" way.*", What? Using `char* names[20]` doesn't remove the ambiguity at all! (And before you say that's not the correct way, [read this](https://stackoverflow.com/a/6990768/589924).) – ikegami May 12 '21 at 16:58