3

Hello guys I recently picked up C programming and I am stuck at understanding pointers. As far as I understand to store a value in a pointer you have to bind memory (using malloc) the size of the value you want to store. Given this, the following code should not work as I have not allocated 11 bytes of memory to store my string of size 11 bytes and yet for some reason beyond my comprehension it works perfectly fine.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){

  char *str = NULL;

  str = "hello world\0";
  printf("filename = %s\n", str);
  return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 3
    You don't need to put `\0` at the end. The string literal does that for you. –  May 10 '17 at 11:29

5 Answers5

3

In this case

 str = "hello world\0";

str points to the address of the first element of an array of chars, initialized with "hello world\0". In other words, str points to a "string literal".

By definition, the array is allocated and the address of the first element has to be "valid".

Quoting C11, chapter §6.4.5, String literals

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.78) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence. [....]

Memory allocation still happens, just not explicitly by you (via memory allocator functions).


That said, the "...\0" at the end is repetitive, as mentioned (in the first statement of the quote) above, by default, the array will be null-terminated.

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

Using a char variable without malloc is stating that the string you are assigning is read-only. This means that you are creating a pointer to a string constant. "hello world\0" is somewhere in the read-only part of memory and you are just pointing to it.

Now if you want to make changes to the string. Let's say changing the h to H, that would be str[0]='H'. Without malloc that will not be possible to make.

Jimmy_A
  • 187
  • 4
  • 13
1

Right. But in this case you are just pointing to a string literal which is placed in the constant memory area. Your pointer is created in the stack area. So you are just pointing to another address. i.e, at the starting address of string literal.

Try using copy the string literal in your pointer variable. Then it will give error because you have not allocated memory. Hope you understand now.

Black_Knight
  • 23
  • 1
  • 6
1

When you declare a string literal in a C program, it is stored in a read-only section of the program code. A statement of the form char *str = "hello"; will assign the address of this string to the char* pointer. However, the string itself (i.e., the characters h, e, l, l and o, plus the \0 string terminator) are still located in read-only memory, so you can't change them at all.

Note that there's no need for you to explicitly add a zero byte terminator to your string declarations. The C compiler will do this for you.

Community
  • 1
  • 1
r3mainer
  • 23,981
  • 3
  • 51
  • 88
0

Storage for string literals is set aside at program startup and held until the program exits. This storage may be read-only, and attempting to modify the contents of a string literal results in undefined behavior (it may work, it may crash, it may do something in between).

John Bode
  • 119,563
  • 19
  • 122
  • 198