3

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?

hmjd
  • 120,187
  • 20
  • 207
  • 252
Dagoth Ulen
  • 520
  • 1
  • 5
  • 12

5 Answers5

7

*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.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • @DagothUlen wait posting a link with example read that also. – Grijesh Chauhan Mar 29 '13 at 17:09
  • @DagothUlen read my [*this answer*](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-returns/15177499#15177499) also – Grijesh Chauhan Mar 29 '13 at 17:10
  • 1
    even better, thanks again :) – Dagoth Ulen Mar 29 '13 at 17:13
  • @GrijeshChauhan u said somearray contains address of 1st element in arry and again u say it gives 423 how? – Maizere Pathak.Nepal Apr 19 '13 at 11:18
  • @Maizere Marizere I said `somearray[]` contains address of `"Hello"` Notice **`[]`**. Second I said `somearray` is address of first elements that first element in array `somearray[]` and `somearray` value is `243` that is correct first element is at 423 location also aray starts at 423 location so first element and array arrdress values are same. but semantically both are difference. Every sentence are correct in answer. [If still it doesn't help read this answer](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-returns/15177499#15177499) . **Good Luck!** – Grijesh Chauhan Apr 19 '13 at 15:23
  • @DagothUlen Read this answer: [Difference between `char* str[]` and `char str[][]` and how both stores in memory?](http://stackoverflow.com/questions/17564608/what-does-the-variable-name-mean-in-case-of-array-of-char-pointers/17661444#17661444) – Grijesh Chauhan Jul 15 '13 at 20:40
4

You're declaring an array of constant strings, allocated on the stack.

You could do this for example:

char* strs[] = { "Hello", "world" };

Then strs[0] would points to the constant string "Hello", and strs[1] to "world".

cmc
  • 2,061
  • 1
  • 19
  • 18
1

I prefer to read it as

char* somearray[]

since you're creating an array of pointers.

Each element in somearray points to a char*.

000
  • 26,951
  • 10
  • 71
  • 101
1

It's an array of strings. It the same as this:

typedef char * string;
string somarray[] = {"Hello"};

That is, each element of somarray is a string. A string, in place is a pointer to many characters.

hdante
  • 7,685
  • 3
  • 31
  • 36
0

What you probably wanted to do is this:

char somearray[] = {"Hello"};

What you did is make a pointer to a pointer by doing:

char *somearray[] = {"Hello"};
spartacus
  • 613
  • 6
  • 12