-2

I was wondering what properties double quotes have, especially in relation to initializing char pointers.

char *ptr="hello";
char array[6]={'h','e','l','l','o'};
cout<<ptr<<endl<<array<<endl;

The above code prints hello twice. I know that using double quotes denotes a string, or a char array with a null character at the end. Since ptr is looking for a memory address (char*) to be assigned to, I'm guessing that the "hello" resolves to the memory address of the 'h', despite the fact that you are also filling in char values for the rest of the array? If this is the case, does that mean that in the above code

char *ptr="hello";

the double quotes creates a string somewhere in memory and then ptr is assigned to the first element of that string, whereas

char array[6]={'h','e','l','l','o'};

creates an array somewhere in memory and then assigns values for each index based on the right hand side of the assignment operator?

glint
  • 1
  • 3
    You need to turn up your compiler warnings. `char *ptr="hello";` should either warn you or error saying it is not allowed. – NathanOliver Feb 22 '18 at 22:46
  • @NathanOliver: Though given that C string literals are not `const` (though it's undefined behavior to modify the contents), `char *ptr = "hello";` is legal in C, and many compilers allow it in C++ (as long as you don't assign it) unless the warnings are turned up to pedantic levels. – ShadowRanger Feb 22 '18 at 22:59
  • 1
    @ShadowRanger The question is not tagged C, and the conversion was deprecated in C++03 (nearly *15 years ago*) and has since been officially removed from the language. – François Andrieux Feb 22 '18 at 23:07

2 Answers2

2

There are two things to note here of importance.

char array[6]={'h','e','l','l','o'};

This will allocate 6 bytes on the stack and initialize them to "hello\0"; The following are equlivalent:

char array[] = "hello";
char array[6] = "hello";

However, the below is different.

char *ptr="hello";

This will allocate a pointer on the stack, that points to the constant string "hello\0". This is an important distinction, if you alter the value ptr points to, you will cause undefined behavior as you will be altering the constant value it points to.

Example:

#include <stdio.h>

void testLocal()
{
  char s[] = "local"; // locally allocated 6 bytes initialized to "local\0"
  s[0] = 'L';
  printf("%s\n", s);
}

void testPointer()
{
  char *s = "pointer"; // locally allocated pointer, pointing to a constant
  // This segfaults on my system, but really the behavior is undefined.
  s[0] = 'P';
  printf("%s\n", s);
}

int main(int argc, char * argv[])
{
  testLocal();
  testPointer();
}
Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • I'm pretty sure you are wrong. First array is just an array, that does not need to be null terminated. Second one just won't compile, unless you change it to `char ptr[] = "foo";`. This compiles only on GCC as an extension AFAIK. – Poeta Kodu Feb 22 '18 at 22:56
  • @razzorflame, Geoffrey is right about `array`. The sixth element is initialized to `0`. – R Sahu Feb 22 '18 at 22:57
  • Yes, it is initialized to 0 but it does not need to be. A char array is not meant to always be zero terminated. – Poeta Kodu Feb 22 '18 at 22:59
  • 1
    @razzorflame, true but in this case there is an extra element in the array, which is required by the language to be zero-initialized. – R Sahu Feb 22 '18 at 23:00
  • 1
    Ok, ignore part of my comment about the first array, he is right. However, assigning string literal to char* is invalid in C++. – Poeta Kodu Feb 22 '18 at 23:02
-1

For strings, there is a special terminal character \0 which is added to the end. This tells it that that's the end of the string.

So, if you have a string "hello", it'll keep reading each character: "h", "e", "l", "l", "o", "\0", which tells it to stop.

The character array is similar, but doesn't have that terminal character. Instead, the length of the array itself is what indicates how many characters to read (which won't necessarily work for all methods).

samanime
  • 25,408
  • 15
  • 90
  • 139