Assigning strings to pointer in C

My question is about pointers in C. As far as I've learned and searched, pointers can only store addresses of other variables, but cannot store the actual values (like integers or characters). But in the code below the char pointer c actually storing a string. It executes without errors and give the output as 'name'.

#include main()

Can anyone explain how a pointer is storing a string without any memory or if memory is created where it is created and how much size it can be created? I tried using it with the integer type pointer

#include main()
but it gave an error
cc test.c -o test test.c: In function ‘main’: test.c:5:3: warning: assignment makes pointer from integer without a cast [enabled by default] c=10; ^ test.c:6:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=] printf("%d",c); ^ 

Pointers stores the address of variable then why is integer pointer different from character pointer? Is there something I am missing?

6,152 2 2 gold badges 26 26 silver badges 27 27 bronze badges asked Jul 11, 2014 at 4:58 185 1 1 gold badge 1 1 silver badge 5 5 bronze badges

It looks like you are using GCC. Always use gcc -Wall -g to compile your code. Don't forget to get all warnings with -Wall and debugging information with -g .

Commented Jul 11, 2014 at 11:15

2 Answers 2

This is just the way string literals work in C. String literals like "name" are arrays of characters, it is equivalent to the five element array . For the code

char *c; c="name"; 

the environment reserves memory for the above array already at initialization time, when the program is loaded from disk into memory. At run time, the adress of the beginning of that array is assigned to c.

Note the first piece of code of yours is not equivalent to the second, because in the first piece you assign a string literal (and not a character like 'n') to a char* variable. In the second, you try to assign an int (and not an int array ) to an int* .

answered Jul 11, 2014 at 5:25 211k 34 34 gold badges 391 391 silver badges 596 596 bronze badges

I'm not sure that initialization time is the good word. I would say compile time. Program initialization is often the beginning of its execution.

Commented Jul 11, 2014 at 11:16

@BasileStarynkevitch: Initially, I was considering to write "compile time", but then noted that this is not really correct. The compiler writes the characters into the binary file of your executable - but the memory to which char*c points is the memory where the operating system places that data when loading the file from disk.

Commented Jul 11, 2014 at 11:23

But the initialization of that (virtual) memory chunk happened when the executable was produced -perhaps many months ago-, not when it runs.

Commented Jul 11, 2014 at 11:24

I wrote about virtual memory (which is the memory relevant for application execution), and you speak of physical RAM. On Linux execve is sort-of mmap -ing the text segment of the ELF executable, and the literal string is a constant inside that text segument

Commented Jul 11, 2014 at 11:28

@BasileStarynkevitch: the compiler (and static linker) may define a memory layout at compile (or link) time, but the reservation of the actual memory in use happens (probably 10 years) later. But you don't have to demonstrate that understanding, I believe you that you have understood the details - this is about a linguistic problem, I think "compile time" is just the wrong term for my answer. Maybe initialization time is not ideal, too, but IMHO still better.

Commented Jul 11, 2014 at 11:45

String literals like "name" are stored as arrays of char ( const char in C++) such that they are allocated when the program starts and held until the program terminates.

The type of the expression "name" is "5-element array of char " (5th element for the 0 terminator). Except when it is the operand of the sizeof or unary * operators, or is a string literal being used to initialize an array in a declaration, an expression of type "N-element array of T " will be converted ("decay") to an expression of type "pointer to T ", and the value of the expression will be the address of the first element of the array.

So, when you write

c="name"; 

"name" is not the operand of the sizeof or unary * operators, and it isn't being used to initialize an array in a declaration, so the address of the first element of the string is being assigned to the pointer variable c . Essentially, what you have in memory is something like the following:

 +-----+ "name"[0] : | 'n' |