Take the 2-minute tour ×

I am a newbie to C programming. And I am confused with the chaotic behavior of pointers. Specially when it comes to strings and arrays.

I know that I can't write like,

#include <stdio.h>
int main()
 int *number=5;

Because clearly it will try to write to the 5th location of the memory.And that will crash the program.I have to initialize the "number".

But when it comes to strings I can write like,

#include <stdio.h>
int main()
 char *name="xxxxx";

And it works too. So that means it implicitly initialize the "name" pointer.I also know that name=&name[0] But I have found that name=&name too. How can it be? Because, to me it looks two variables with the same name. Can anybody tell me how strings are created in memory?(All this time time I assumed it creates name[0].....name[n-1] and another variable(a pointer) called "name", inside that we put the location of name[0].Seem to be I was wrong.)

PS:-My English may not be good and if somebody can give me a link regarding above matter that would be grateful.

share|improve this question
The behaviour of pointers in C is not chaotic. It is well defined. –  Daniel Daranas May 2 '13 at 8:50
name is definitely different from &name if name is of type char*. it is only true if name is of char[] and that too only the value is same but not the type. for char name[] name is of char[] type but &name is of char(*)[] –  Koushik May 2 '13 at 9:13
Also, please ask a single question at a time, instead of putting everything that puzzled you in the same question. –  Daniel Daranas May 2 '13 at 9:20
sorry for putting several questions together.When something is unsure it raises more questions along with it?But thanks anyway. –  user2247911 May 2 '13 at 11:29

5 Answers 5

C, like many programming languages, supports the concept of "literals" - special syntax that, when encountered, causes the compiler to create a value in a special way.

-2, for example, is an integer literal. When encountered, the compiler will treat it as a value of type int with the content of -2. "..." is a string literal - when encountered, the compiler allocates new space in a special memory area, fills it with data that corresponds to the chars you used inside the literal, adds a 0 at the end of that area, and finally uses a pointer to that area, of type char*, as the result of the literal expression. So char* s = "hello" is an assignment from something of type char* into a variable of type char* - completely legal.

You sneaked in another question here - why a == &a[0]. It's best to ask one question at a time, but the gist of it is that a[n] is identical to *((a)+(n)), and so:

&a[0] == &*(a+0) == a+0 == a
share|improve this answer
"-2" looks like string literal:-). perhaps you could use ``. –  Koushik May 2 '13 at 9:02
@Koushik right you are, fixed! –  Oak May 2 '13 at 9:04
actually i was wondering about how a=&[a]? –  user2247911 May 2 '13 at 11:58
@user2247911 I don't know what &[a] is... in any case, it's best to ask it separately as a new question. –  Oak May 2 '13 at 12:27
sorry about above statement. it should be a=&a.My mistake.But don't worry about that because later i realized you have already given me the answer i expected. –  user2247911 May 3 '13 at 4:26

Basicly what happens when the C-compiler see the expression

char *name = "xxxxx";

is, it will say. Hey "xxxxx" that's a constant string (which is an array of bytes terminated with a 0 byte), and put that in the resulting programs binary. Then it will substitute the string for the memory location, sort of like:

char *name = _some_secret_name_the_compiler_only_know;

where _some_secret_name_the_compiler_only_know is a pointer to the memory location where the string will live once the program gets executed. And get in with parsing the file.

share|improve this answer

Let's consider this piece of code:

 char *name="xxxxx";

What happens here is that the string xxxxx is allocated memory and a pointer to that memory location,or the address of that string is passed to the pointer variable name.Or in other words you initialize name with that address.

And printf() is a variadic function (one that takes one fixed argument followed by a random number of arguments).The first argument to printf() is of type const char*.And the string identifier name when passed as an argument denotes the base address of that string.Hence you can use


It will simply output xxxxx

name=&name too. How can it be?--Well that's a justified question.

Let me explain first with a real-world analogy. Suppose you have the first house in a row of houses in a housing society.Suppose it's plot number 0.The other houses are on plots 1,2,3......Now suppose there is a pointer to your house, and there is another pointer to the whole housing society's row. Won't the two pointers have the same address, which will be plot 0?This is because a pointer signifies a single memory location.It's the type of the pointer that matters here.

Bringing this analogy to the string( array of characters), the name identifier only signifies the base address of the string, the address of its first character (Like the address of the first house).It is numerically same to the address of the whole string,which is (&name),which in my analogy is the ROW of houses.But they are of different types, one is of type char* and the other is of type char**.

share|improve this answer
Downvoter, stop your "revenge" downvoting.Show me where I am wrong if you feel I am. –  T Singh May 2 '13 at 9:11
I didn't vote on your answer at all. And I deleted my comments on where you had a tiny mistake, after you corrected it. So if by chance you meant me by "revenge": I didn't I'm not into that. –  kratenko May 2 '13 at 9:20
 char *name="xxxxx";

This creates a char array(const) in memory, and it will assign the address of 1st element of it to name. char* array names are like pointers. &name[0] means address of 1st element and name (In c,cpp just the char * array name will provide you with the address of 1st element too (bcos that is what was assigned to name in the 1st place itself)) also gives the same result.

The notation name[i] is translated as *(name+i) so u actually have a base address name to which you add subscripts. (calculations are as per pointer arithmetic) . printf("%s", name) is designed to print from start address to \0 (which is appended to the end of strings created using char* a.k.a String literals)

Check this too.

share|improve this answer

That is because you those strings are actually arrays of characters. What you do with the line char *name = "xxxxx";: You construct an array of 6 character values (5 of them being 'x' and the last one being '\0'). name is a pointer to that array.

That's how strings are normally handles in C, you have some kind of sequence of characters, terminated with a '\0' character, to tell functions like printf where to stop processing the string.

share|improve this answer
you could also add that printf(name) is bad practice –  Koushik May 2 '13 at 8:57

Your Answer


