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

Some code I read had an initialization statement like this

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

      

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

--- Edited --- and please, what's the difference in this example between char array [3]; and also char * array [3]; --- Edited ---

+3


source to share


5 answers


what does it mean?

It initializes an array of strings ( char *

) with three values ​​(three pointers to null terminated strings)

and what does this pointer point to?

It must point to the first element of the array char*

how is it allocated in memory?

It will allocate enough memory to store three lines followed by null terminators, as well as three pointers to those lines:

array --> pointer to three sequential memory addresses

array[0] --> something1{\0}
array[1] --> something2{\0}
array[2] --> something3{\0}

      



Note that strings may not need to be in sequential memory

and how can i access each element

If by "element" you mean a string, you can loop over pointers:

for(int i=0; i<3; i++)
{
    char* element = array[i];
}

      

and each element character in this array

well, you can access characters using the array ( element[i]

) syntax , but I would recommend using the C string functions for security (so you don't have to worry about accessing memory outside of the string range)

+5


source


This is a way to initialize an array at the same time 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's 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

.

+4


source


char *

in C, a string.

array

is the name of the variable being declared.

[]

indicates that this is an array.

{ "something1", "something2", "something3" }

initializes the contents of the array.

The elements are accessed as follows:

array[0]

gives the 1st element - "something1".

array[1]

gives the second element, "something2".

and etc.

Note:

As noted in the comments, char *

not technically a string.

This is a pointer to char

. You can render a string in memory like this:

<-------------------------->
..134|135|136|137|138|139|..
<-------------------------->
  'H'|'e'|'l'|'l'|'o'|'\0'
<-------------------------->

      

This block of memory (positions 134-139) contains a character string.

For example:

array[0]

actually returns a pointer to the first character in "something1".

You use the fact that characters are stored 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];

      

+3


source


Defines an array of char pointers (aka "c strings").

To access content you could do:

for (int i=0; i<sizeof(array)/sizeof(char*); i++) {
    printf("%s", array[i]);
}

      

+2


source


It declares array

as an array of 3 pointers to char

, with its 3 elements initialized with pointers to the corresponding strings. Memory is allocated for the array itself (3 pointers) and for strings. Row memory is allocated statically. The array memory is allocated either statically (if the declaration is outside of all functions) or dynamically (usually on the CPU execution stack) if the declaration is inside a function.

0


source







All Articles