Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does that mean in c? char *array[]= { "**", "**", "**" };

Tags:

c

In some code that I read, there was an initializing statement like this

char *array[]= { "something1", "something2", "something3" };

What does this mean, and what does that pointer actually point to? How is that allocated in memory, and how can I access every element and every character of an element in that array ?

--- Edited --- and please what is the difference in this example between char array[3]; and char *array[3]; --- Edited ---

like image 389
Muhammad Barrima Avatar asked Jan 28 '13 20:01

Muhammad Barrima


3 Answers

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)

like image 185
D Stanley Avatar answered Oct 26 '22 17:10

D Stanley


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.

like image 42
Jeff Wolski Avatar answered Oct 26 '22 18:10

Jeff Wolski


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];
like image 43
Bertie Wheen Avatar answered Oct 26 '22 18:10

Bertie Wheen