-3

I have not been able to find an answer to this question (I have looked so apologies if it is a repeat). In C when initializing a char array there are roughly two methods as I know it:

char[] a = "Hello";

char * b = "Hello";

These achieve the same result but the second initialization for 'b' confuses me. I understand the initialization for 'a' as it is the somewhat the equivalent to *(char *a) = "Hello" assuming that memory has been allocated etc etc ... But the syntax for the initialization of 'b' looks to me like 'b' is a pointer, pointing to an address "Hello" which of course makes no sense unless the ASCII values are being used. As far as I have seen there are no equivalents to 'b's initialization for intergers or another data type. If anyone could offer an explanation that would be great ! I have been using C for a long time and this one has always slightly bugged me and I have not been able to think of/find an answer

  • 4
    Which are you actually using: C or C++? – Scott Hunter Mar 09 '22 at 18:22
  • 3
    Are you asking how this works in C or in C++? – Ruurd Adema Mar 09 '22 at 18:22
  • 5
    There is no laguage called "C/C++". And the answer will be different for C and C++. – Yksisarvinen Mar 09 '22 at 18:23
  • `a` points to a string allocated on the stack (whose contents are therefore modifiable), while `b` points to a string allocated in the read-only data section of the executable image (whose contents are therefore non-modifiable). The initialization of `a`, BTW, executes data copy every time the function is called (in other words, the string itself is in the RO section in both cases, but in the case of `a`, it is copied from that section to the stack every time the function is called). –  Mar 09 '22 at 18:25
  • Related: https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-char-s-initialized-with-a – Barmar Mar 09 '22 at 18:27
  • Does this answer your question? [Why do I get a segmentation fault when writing to a "char \*s" initialized with a string literal, but not "char s\[\]"?](https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-char-s-initialized-with-a) – Mgetz Mar 09 '22 at 18:28
  • Here's a [related](https://stackoverflow.com/q/10186765/14749654) question you might want to look at. – Monir John Rakib Mar 09 '22 at 18:31
  • @Mgetz that is exactly what I was looking for but couldn't find the question. Thank you so much ! – RickarySanchez Mar 10 '22 at 10:20

2 Answers2

5

These achieve the same result

Not quite.

char[] a = "Hello";

understand the initialization for 'a' as it is the somewhat the equivalent to *(char *a) = "Hello"

*(char *a) = "Hello" is non-sensical to me, so I wouldn't agree it to be equivalent. a is an array. Its size is deduced from the initialiser (6 elements).

char * b = "Hello";

But the syntax for the initialization of 'b' looks to me like 'b' is a pointer,

Correct. b is a pointer, Hence why it is different from a which is an array.

This one is ill-formed in C++. This is because string literals are arrays of const char in C++ and those don't implicitly convert into pointers to non-const char (since C++11).

pointing to an address "Hello" which of course makes no sense

Incorrect. b points to the address of the first element of "Hello".

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

"Hello" is a string literal. In an asm dump, you will see that the memory for it will be reserved in the .data section. So I think that b is just a pointer to preallocated and initialized memory.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278