-3

I'm new comer to C++, so I'm very confused about the code below:

const char* headers[]= {"apple","pear","tree"};

So is this line supposed to create "an array of pointers to char"? (Maybe i'm wrong). If so, why such array of pointers being assigned to an array of strings?

Also I'm confused why bother to have such pointer array? Can't we simply do sth like: int array [5] = { 1, 2, 3, 4, 5 };

All such pointer/reference stuff in C++ are so confusing and difficult for people like me from Java world.

Many thanks!

LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
  • 1
    first time today - use std::vector, life always goes smoother with it – pm100 Mar 13 '18 at 18:34
  • 2
    If you want a Java equivalent use `std::vector` or `std::array`. The array your looking at is a Cism – NathanOliver Mar 13 '18 at 18:34
  • 1
    ***If so, why such array of pointers being assigned to an array of strings?*** This is not the way `c++` programmers would do this. – drescherjm Mar 13 '18 at 18:36
  • are you clear on pointer? – AsthaUndefined Mar 13 '18 at 18:36
  • Can anyone explain what "char* headers[]" mean? Thanks – LookIntoEast Mar 13 '18 at 18:37
  • Read about that in a `c` book. When you finally get to `c++` you will use `std::string` of c-strings. – drescherjm Mar 13 '18 at 18:37
  • `why such array of pointers being assigned to an array of strings` They are not strings, they are arrays of `const char`s – Killzone Kid Mar 13 '18 at 18:37
  • 5
    "All such pointer/reference stuff in C++ are so confusing and difficult for people like me from Java world." On behalf of the C++ community I apologise for it. – n. m. could be an AI Mar 13 '18 at 18:39
  • I see no Cism here. In some cases, using it like that could be justified, (likely with constexpr) as opposed to using an array of `std::string`. – SergeyA Mar 13 '18 at 18:42
  • 1
    @user815408 Try use the C++ `std::vector` class: `std::vector headers {"apple","pear","tree"};` for a Java "equivalent". Anyway like others mentioned you should study C++ pointers and "c strings", so you can understand your question. – RandomGuy Mar 13 '18 at 18:45
  • 2
    C++ is not a do-what-I-mean programming language. You'll need [books](https://stackoverflow.com/q/388242/1889329) to study. – IInspectable Mar 13 '18 at 18:45
  • @IInspectable I am in dire need of do-what-I-mean language. Can you please suggest one? – SergeyA Mar 13 '18 at 18:50
  • @SergeyA, you will have to wait for telepathic compiler support to become mandatory in the C++4574 standard. – user4581301 Mar 13 '18 at 19:16

3 Answers3

3
const char* headers[]= {"apple","pear","tree"};

"If so, why such array of pointers being assigned to an array of strings?"

Because in C there is no such thing as a string. There is instead a sequence of characters with a trailing 0 at the end.

C++ has std::string.

const char* headers[]= {"apple","pear","tree"};

means headers is an array of char pointers. Simple way to dissect it to look at non array version

const char * fruit = "pear";

This creates a literal "pear\0" (note the 0 added on the end) and then creates a variable that points to the 'p' character. Ie it contains the address of where that 'p' is stored. Now back to

const char* headers[]= {"apple","pear","tree"};

This creates 3 literals 'apple\0', 'pear\0', 'tree\0'

It create a 3 entry array of char * pointers

It sets header[0] to point to the 'a' of apple

It sets header[1] to point to the 'p' of pear

It sets header[2] to point to the 't' of tree

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Thanks. But I'm still confused about what does "char* headers[]" mean? – LookIntoEast Mar 13 '18 at 18:40
  • 2
    Pointers to a C array point to the first element of the array (one char in this case). So It declares an array of pointers to chars. – stark Mar 13 '18 at 18:43
3

Hope this will help, here is a basic explanation of what the line means with some crude illustrations. Please note that all addresses are made up, and some things are simplified in hopes of making the concept easier to see. Forgive the crude art, I am far from an artist! const char* headers[]= {"apple","pear","tree"};

This line is telling the compiler that you would like to allocate an array. The elements in the array will be of type char*. The const keyword tells the compiler that the object or variable is not modifiable. char* is the type of elements in the array. headers is the variable name, and the brackets [] indicate that headers is an array. The rest of the line is an initializer for the array. header will have 3 elements of type char* which basically means “pointer to a string of characters”.
The compiler will also allocate memory and initialize the array by filling it with each of the 3 strings in the braces {"apple","pear","tree"}

The first element in the header array header[0] will contain a pointer to the memory where the string “apple” is located. The second element in the header array header1 will contain a pointer to the memory where the string “pear” is located.

enter image description here

MEHM-
  • 98
  • 5
2

Answering the question as asked:

const char* headers[]= {"apple","pear","tree"};

This line defines a variable named headers, with type "array of 3 pointers to const char" (where 3 is deduced from the number of initializers). The pointers are set to point to 3 string literals, which they remain immutable during program execution.

SergeyA
  • 61,605
  • 5
  • 78
  • 137