In some code that I read, there was an initializing statement like this
char *array[]= { \"something1\", \"something2\", \"something3\" };
what that means ?
It's initializing an array of strings (char *
) with three values (three pointers to null-terminating strings)
and what that pointer points to ?
It should point to the first element in the char*
array
how is that allocated in memory ?
It will allocate enough memory to store the three strings followed by null-terminators, as well as the three pointers to those strings:
array --> pointer to three sequential memory addresses
array[0] --> something1{\0}
array[1] --> something2{\0}
array[2] --> something3{\0}
Note that the strings may not necessarily be in sequential memory
and how can I access every element
if by "element" you mean the string, you can loop though the pointers:
for(int i=0; i<3; i++)
{
char* element = array[i];
}
and every character of an element in that array
well, you could access the chars using array syntax (element[i]
) but I would recommend using the C string functions for safety (so you don't have to worry about accessing memory outside the range of the string)
This defines a array of char pointers (aka. "c strings").
For accessing the content you could do:
for (int i=0; i<sizeof(array)/sizeof(char*); i++) {
printf("%s", array[i]);
}
This is a way to initialize an array at the same time that you create it.
This code
char *array[]= { "a", "b", "c" };
will have the same result as this code.
char *array[3];
array[0] = "a";
array[1] = "b";
array[2] = "c";
Here is a good source for more information.
http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Strings
EDIT:
char array[3];
is an array of 3 char
.
char *array[3];
is an array of 3 pointers to char
.
char *
in C is a string.
array
is the name of the variable being declared.
[]
indicates that it is an array.
{ "something1", "something2", "something3" }
is initializing the contents of the array.
Accessing elements is done like so:
array[0]
gives the 1st element - "something1".
array[1]
gives the 2nd element - "something2".
etc.
Note:
As was pointed out in the comments, char *
isn't technically a string.
It's a pointer to a char
. You can visualize a string in memory like so:
<-------------------------->
..134|135|136|137|138|139|..
<-------------------------->
'H'|'e'|'l'|'l'|'o'|'\0'
<-------------------------->
This block of memory (locations 134-139) is holding the string of characters.
For example:
array[0]
actually returns a pointer to the first character in "something1".
You use the fact that the characters are sequentially in memory to access the rest of the string in various ways:
/* ch points to the 's' */
char* ch = array[0];
/* ch2 points to the 'e' */
char* ch2 = ch + 3;
/* ch3 == 'e' */
char ch3 = *ch2;
/* ch4 == 'e' */
char ch4 = *(ch + 3);
/* ch5 == 'e' */
char ch5 = ch[3];
It declares array
as an array of 3 pointers to char
, with its 3 elements initialized to pointers to the respective strings. The memory is allocated for the array itself (3 pointers) and for the strings. The strings memory is allocated statically. The array's memory is allocated either statically (if the declaration is outside of all functions) or dynamically (typically, on the execution stack of the CPU) if the declaration is inside a function.