4

I'm quite confused because from what I've learned, pointers store addresses of the data they are pointing to. But in some codes, I see strings often assigned to pointers during initialization.

What exactly happens to the string?
Does the pointer automatically assign an address to store the string and point itself to that address?
How does "dereferencing" works in pointers to strings?

haccks
  • 104,019
  • 25
  • 176
  • 264
Euhi
  • 51
  • 2

2 Answers2

8

In case of

char *p = "String";

compiler allocate memory for "String", most likely "String" is stored in read only data section of memory, and set pointer p to points to the first byte of that memory address.

p --------------+
                |
                |
                V
             +------+------+------+------+------+------+------+
             |      |      |      |      |      |      |      |
             | 'S'  | 't'  | 'r'  | 'i'  | 'n'  | 'g'  | '\0' |
             |      |      |      |      |      |      |      |
             +------+------+------+------+------+------+------+
              x100    x101   x102   x103   x104   x105   x106
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • also I think you should also update the answer with how here in this case a `\0` is stored to mark the end of the string and how this is used internally by all the functions to print and consume the string. – Krishna Oza Nov 26 '15 at 10:33
  • 1
    @Krishna_Oza How is that relevant? The question is about allocation of string literals and pointers to them. – Lundin Nov 26 '15 at 10:36
  • 1
    Sir, `String is stored in read only data section`.....anything specified in the standard? – Sourav Ghosh Nov 26 '15 at 10:38
  • 2
    @SouravGhosh The standard doesn't dictate where to allocate things, it only states that writing to string literals is undefined behavior. Therefore the memory where a string literal is stored can always be regarded as read-only, even if the target system lacks true ROM. The memory segment is usually called `.rodata`. – Lundin Nov 26 '15 at 10:44
  • @SouravGhosh; As Lundin pointed in his comment that it is not mentioned specifically in standard, but it is not necessary that string literals will go to read-only section of memory. It is platform dependent. – haccks Nov 26 '15 at 10:57
4

Q: I see strings often assigned to pointers during initialization.

I think, what you are calling as string is actually a string literal.

According to C11 standard, chapter §6.4.5

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz". [...]

The representation, "xyz" produces the address of the first element of the string literal which is then stored into the pointer, as you've seen in the initialization time.

Q: Does the pointer automatically assign an address to store the string and point itself to that address?

A: No, the memory for storing the string literal is allocated at compile time by the compiler. Whether a string literal is stored in a read only memory or read-write memory is compiler dependent. Standard only mentions that any attempt to modify a string literal results in undefined behavior.

Q: How does "dereferencing" works in pointers to strings?

A: Just the same way as it happens in case of another pointer to any other variable.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261