2

Just starting out in C++, I was wondering if someone could explain something.

I believe you can initialise a char array in the following way

 char arr[] = "Hello"

This will create a Char array with the values 'H', 'e', 'l', 'l', 'o', '\0'.

But if I do create this:

 char* cp = "Hello";

Will that create an array, and the pointer to that array?

Eg: cp will point to the first element ('H') in memory, with the additional elements of the array?

AJ9
  • 1,256
  • 1
  • 17
  • 28
  • Take a look here: http://stackoverflow.com/questions/10186765/char-array-vs-char-pointer-in-c – Araw May 21 '13 at 19:25
  • `char* cp = "Hello"` is incorrect, it should be `const char* cp = "Hello"` – Andrei May 21 '13 at 19:25
  • Have you tried to check what you will find in memory if you follow the pointer? If not, do that. Also, try the search feature here on SO and you'll find lots of questions, with good answers, explaining everything about pointers and arrays in C/C++. – HonkyTonk May 21 '13 at 19:26

2 Answers2

3

The string literal itself has array type. So in the first example you gave, there are actually two arrays involved. The first is the array containing the string literal and the second is the array arr that you're declaring. The characters from the string literal are copied into arr. The C++11 wording is:

A char array (whether plain char, signed char, or unsigned char), char16_t array, char32_t array, or wchar_t array can be initialized by a narrow character literal, char16_t string literal, char32_t string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces. Successive characters of the value of the string literal initialize the elements of the array.

In the second example, you are letting the string literal array undergo array-to-pointer conversion to get a pointer to its first element. So your pointer is pointing at the first element of the string literal array.

However, note that your second example uses a feature that is deprecated in C++03 and removed in C++11 allowing a cast from a string literal to a char*. For valid C++11, it would have to instead be:

const char* cp = "Hello";

If do use the conversion to char* in C++03 or in C, you must make sure you don't attempt to modify the characters, otherwise you'll have undefined behaviour.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • 3
    The question is also tagged C, so you may add that in C, `char*` is perfectly valid (although, since attempting to modify string literals invokes undefined behaviour, using `const char*` has advantages also in C). – Daniel Fischer May 21 '13 at 19:31
  • @DanielFischer I should look at the tags more. Thanks. – Joseph Mansfield May 21 '13 at 19:31
  • Would the second example allow you to edit the contents of the pointed to array? Eg: cp = "Apple". I was unsure if the array would be generated automatically, thanks for the response! – AJ9 May 21 '13 at 19:38
  • @Pillbox No. If you attempt to modify the array, you'll have undefined behaviour. String literals are typically stored as read-only. – Joseph Mansfield May 21 '13 at 19:40
  • 4
    @Pillbox But `cp = "Apple";` wouldn't change the contents of the array, it would change _which_ array's initial element `cp` points to. So `cp = "Apple"; cp = "Banana";` is okay. `cp[0] = 'x';` would invoke undefined behaviour. – Daniel Fischer May 21 '13 at 19:44
  • 1
    @DanielFischer You're noticing lots of things that I'm not. Thanks. – Joseph Mansfield May 21 '13 at 19:45
  • would `int*b = {1,2,3}` work? – Suraj Jain Dec 27 '16 at 09:43
-1

An array is basically a constant pointer, which points to the beginning of an array. A pointer is just a pointer, which points to any memory location. So given the pointer p, p[3] would point to p+3, which would give a segmentation fault, unless you had declared it as an "array" with at least 4 elements(int *p = new int[4];). This is exactly the same for int p[4];, except the fact that p is now a const int *.

Anickyan
  • 404
  • 2
  • 10