4

I'm relativly new to Coding and have some Problems understanding how pointers work in combination with arrays (whom on their own I i understand).

I understood that it's possible to create an array of pointers like:

#include <iostream>
using namespace std;

int main() {
    int i;
    int *pArr[10];
    pArr[0]=&i;
    return 0;
}

In a Tutorial I then found the following code:

#include <iostream>
using namespace std;

int main() {
    char *names[4] = {
        "name A",
        "name B",
        "name C",
        "name D"
};

for (int i = 0; i < 4; i++) {
    cout << names[i] << endl;
}
return 0;
}

Why is it, that I can assign Multiple chars, or to say a string, like "name A" to a pointer which should point to a char.

Shouldn't I A:

Only be able to assign the Address of a char to each of those 4 pointers I created.

And B:

Only be able to assign a pointer, to one single letter (a char), to each one.

I hope someone can help clear my confusion to some degree.

7
  • 1
    "name A"'s type actually is const char*. Commented Mar 4, 2017 at 9:32
  • 1
    1. Please read up on const 2. using std is bad - google that as well Commented Mar 4, 2017 at 9:32
  • See some useful compiler warnings here: coliru.stacked-crooked.com/a/f67971a42cebd76f Commented Mar 4, 2017 at 9:41
  • If your tutorial has code from the second snoppet, you need a better tutorial. It's not legal C++ and should not compile cleanly. Commented Mar 4, 2017 at 10:34
  • @πάνταῥεῖ in fact it is const char[7] Commented Mar 4, 2017 at 11:25

4 Answers 4

7

This is a shortcut offered by the compiler to make it easier for programmers to include strings in their code.

When you write a string literal "name A", the compiler prepares a seven-character array for you:

const char hidden0[] = {110, 97, 109, 101, 32, 65, 0}; // "name A"

The first six numbers correspond to character codes of symbols in the string. The last character is zero - the so-called null terminator.

The compiler does the same for all four string literals, so your array initializer looks like this:

const char *names[4] = {
    &hidden0[0],
    &hidden1[0],
    &hidden2[0],
    &hidden3[0]
};

hiddenK is the array created for the corresponding string literal. It has no name, but the compiler knows its address, and places it in the name[] array.

Sign up to request clarification or add additional context in comments.

Comments

3

In C (and C++) a string literal (an expression such as "name A") has type const char*. Under the hood, these string characters are stored somewhere in data section in the binary. And the string literal is substituted with the pointer to the first of these characters. So, the following statement

char *names[4] = {
    "name A",
    "name B",
    "name C",
    "name D"
};

Instructs the compiler to allocate four strings in the data section, and then to assign four pointers to the pointer array names.

3 Comments

In C++, string literal can be assigned to const char*, but in C you can assign it to char* as well. However, this is not the type of string literal - string literals are not character pointers, they are character arrays.
To second that... std::cout << typeid( "foo" ).name() prints "char const [4]". So the right explanation would be that it is a character array and the compiler will substitute the array with a pointer to the first character.
The string literal type is const char[7], not const char * The conversion from array to a pointer prvalue only happens in some contexts
2

You have created an array of 4 pointer to char.

A "string litteral" is an array of char. And the address of this array is a pointer to char. So no problem to put it in your array of pointers.

Now does your code compile ? Or does it complain about mixing of char* and const char* ? This would be another question for which there are already plenty of answers on SO

1 Comment

Prior to C++11 it was permitted (but deprecated) to assign a string literal to char *
1

Each char* points to the first character in the string. When you see a literal string embedded "mystring", then a char* is pointing to the "m" of "mystring".

The cout command is built such that when you pass it a char* variable, it prints all characters starting from that memory address until it hits a zero byte (binary value 0, not the written character "0").

The end result of this is that you can actually increment the char* pointer to get shorter strings. e.g. if you add 1 to a pointer pointing to "mystring" it would now point to "ystring". So your assumption was basically correct, a char* points at one character, not the whole word.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.