Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am a new learner of C language, my question is about pointers. As far i 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 <stdio.h>
main()
{
char *c;
c="name";
puts(c);
}

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 <stdio.h>
main()
{
int *c;
c=10;
printf("%d",c);
}

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.

If there is something i am missing about of pointers plz explain.

Thanks in advance.

share|improve this question
    
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. –  Basile Starynkevitch Jul 11 at 11:15

2 Answers 2

up vote 4 down vote accepted

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 {'n', 'a', 'm', 'e', '\0'}. 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*.

Here is a tutorial on strings and pointers in C with a more detailed explanation.

share|improve this answer
    
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. –  Basile Starynkevitch Jul 11 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. –  Doc Brown Jul 11 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. –  Basile Starynkevitch Jul 11 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 –  Basile Starynkevitch Jul 11 at 11:28
    
@BasileStarynkevitch: you are intermixing virtual memory (which is memory the os provides for the execution) with the binary bytes of the executable. –  Doc Brown Jul 11 at 11:29

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' |  <-------+
            +-----+          |
"name"[1] : | 'a' |          |
            +-----+          |
"name"[2] : | 'm' |          |
            +-----+          |
"name"[3] : | 'e' |          |
            +-----+          |
"name"[4] : |  0  |          |
            +-----+          |
              ...            |
            +-----+          |
        c : |     | ---------+
            +-----+
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.