Home > Back-end >  what is the relation between these char str[10], char *str and char *str[10] in C?
what is the relation between these char str[10], char *str and char *str[10] in C?

Time:06-15

Can I consider *str[10] as two dimensional array ? If I declare char *str[10]={"ONE","TWO","THREE"} how we can access single character ?

CodePudding user response:

This record

char str[10];

is a declaration of an array with 10 elements of the type char, For example you can initialize the array like

char str[10] = "ONE";

This initialization is equivalent to

char str[10] = { 'O', 'N', 'E', '\0' };

all elements of the array that are not explicitly initialized are zero-initialized.

And you may change elements of the array like

str[0] = 'o';

or

strcpy( str, "TWO" );

This record

char *str;

declares a pointer to an object of the type char. You can initialize it for example like

char *str = "ONE";

In this case the pointer will be initialize by the address of the first character of the string literal.

This record

char * str[10];

is a declaration of an array of 10 elements that has the pointer type char *.

You can initialize it as for example

char * str[10] = { "ONE", "TWO", "THREE" };

In this case the first three elements of the array will be initialized by addresses of first characters of the string literals specified explicitly. All other elements will be initialized as null pointers.

You may not change the string literals pointed to by elements of the array. Any attempt to change a string literal results in undefined behavior.

To access elements of the string literals using the array you can use for example two subscript operator. For example

for ( sisze_t i = 0; str[0][i] != '\0';   i )
{
    putchar( str[0][i] );
}
putchar( '\n' );

If you want to change strings then you need to declare for example a two dimensional array like

char str[][10] = { "ONE", "TWO", "THREE" };

In this case you can change elements of the array that are in turn one-dimensional arrays as for example

str[0][0] = 'o';

or

strcpy( str[0], "FOUR" );

CodePudding user response:

Yes: char* str[10]; would create an array of 10 pointers to chars.

To access a single character, we can access it like a 2 dimensional array; i.e.:

char* str[10]={"ONE","TWO","THREE"};
char first = str[0][0];

CodePudding user response:

Can I consider *str[10] as two dimensional array ?

It's unclear what you mean. *str[10] is not a valid type name, and the context is a bit lacking to determine how else to interpret it.

If you mean it as an expression referencing the subsequent definition then no, its type is char, but evaluating it produces undefined behavior.

If you are asking about the type of the object identified by str, referencing the subsequent definition, then again no. In this case it is a one-dimensional array of pointers to char.

If I declare char *str[10]={"ONE","TWO","THREE"} how we can access single character ?

You can access one of the pointers by indexing str, among other other ways. For example, str[1]. You can access one of the characters in the string into which that pointer points by using the indexing operator again, among other ways. For example, str[1][0]. That you are then using a double index does not make str a 2D array. The memory layout is quite different than if you declared, say, char str[3][10];.

  • Related