2

I have just started learning C from David Griffiths Head first C book and I am confused in the section on pointers.

The problem :

This makes sense

char msg[] = "hello";

This also makes sense

char *ptr_of_msg = msg;

because array variables are similar to pointers

But how does this work ? :

char *ptr = "hello";

The thing on the right side is an actual array, how can it be assigned to a pointer ?

  • 1
    A string is just an array of characters. A string literal is the same just pointing to a memory location in the assembly assumed to be readonly. – Matthew Jun 23 '21 at 20:46
  • 2
    1) TL;DR: your first and third examples are simply equivalent. 2) You said "array variables are similar to pointers". More specifically, "arrays can decay to a pointer". Look here for more information: https://stackoverflow.com/a/1461580/421195 – paulsm4 Jun 23 '21 at 20:50
  • 3
    @paulsm4 They aren't equivalent; `msg` is modifiable, the string pointed to by `ptr` is not – Govind Parmar Jun 23 '21 at 20:51
  • Arrays are not pointers and pointers are not arrays, though on access, an array will be converted to a pointer to its first element. [6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) An array is a specific object where all elements are guaranteed to be sequential in memory. A pointer is just a normal variable that holds as its value a memory address where something else can be found. Your "How does this work?" is simply `char *ptr;` and `ptr = "hello";`. Separate the declaration from the assignment and it becomes clear. – David C. Rankin Jun 23 '21 at 20:54
  • There's a couple of nuances here that are a little special that i don't know are really covered in the duplicate. The first one is that `msg` is actually getting a COPY of "hello" allocated into memory, and then is pointing to the copy. That's a special case when you instantiate an array. Once an array is instantiated, you'll can't just use = to copy over members. Second big one is what Govind pointed out: `*ptr` is pointing to the read-only string "hello". You won't be able to modify its members (unless memory is weird on your system). – yhyrcanus Jun 23 '21 at 21:04
  • @yhyrcanus I believe the standard explicitly makes it read-only (though I don't have the exact section/paragraph reference at hand) – Govind Parmar Jun 23 '21 at 21:07
  • @DavidC.Rankin i think one would usually expect something like : `*ptr = "hello"` or `ptr = &variable` but I guess the string literal is just equivalent to its pointer – Ajaykrishnan R Jun 23 '21 at 21:12
  • @Matthew so this means that it only works for strings and not for say int because "hello" is pointing to a location in memory while 4 would just be 4 ? – Ajaykrishnan R Jun 23 '21 at 21:25
  • https://en.cppreference.com/w/c/language/string_literal – Chef Gladiator Jun 24 '21 at 02:48
  • 1
    Let's complicate this further `"hello"` is a *String Literal*. Technically it is an array. It is created in read-only memory on all but a very few systems. When accessed, it is converted to a pointer to the first character. So it has type `const char *`. When you declare `char *ptr = "hello";` due to historic C syntax, you are not required to make it `const` qualified, but it is better to do so. Either way, whether you assign the pointer to the first char in `"hello"` or `&somevar`, you are simply assigning an address to `ptr`. – David C. Rankin Jun 24 '21 at 03:11
  • Let me repeat, please [read this first](https://en.cppreference.com/w/c/language/string_literal). Literally (pun intended) all you need to know. – Chef Gladiator Jun 24 '21 at 12:11

0 Answers0