In c, if i declare something like:
char *somarray[] = {"Hello"};
What does it mean ?
If i print it:
somarray -> gives me a memory address in the stack
&somarray -> same thing, stack memory address, but..
*somarray -> gives me a memory address in the constants
I can actually use *somarray to print the string.
What is going on?
*array[] means array of pointers, in your example:
char *somarray[] = {"Hello"};
somarray[] is array of char*. this array size is one and contains address to on string "Hello" like:
somarray[0] -----> "Hello"
somarray means address of first element in array.
&somarray means array address
*somarray means value of first element
Suppose address of "Hello" string is for example 201, and array somaaray at 423 address, then it looks like:
+----+----+----+---+---+----+----+----+---+----+
| `H`| 'e'|'l'|'l'|'o'| '\0'|
+----+----+----+---+---+----+----+----+---+---+----+
201 202 203 204 205 206 207 208 209 210 2
^
|
+----+----+
| 201 |
+----+----+
423
somarray
and:
somarray gives 423
&somarray gives 423
*somarray gives 201
Point to be notice somarray and &somarray gives same value but semantically both are different. One is address of first element other is address of array. read this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With