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?

2
  • why is this being closed as not constructive ? Commented Mar 29, 2013 at 17:23
  • This should have been closed a "duplicate" rather than "not constructive". Commented May 21, 2013 at 19:39

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@DagothUlen wait posting a link with example read that also.
@DagothUlen read my this answer also
even better, thanks again :)
@GrijeshChauhan u said somearray contains address of 1st element in arry and again u say it gives 423 how?
@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 . Good Luck!
|
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".

Comments

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

Comments

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.

Comments

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"};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.