Reputation: 41
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.
Upvotes: 4
Views: 5992
Reputation: 5269
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
.
Upvotes: 3
Reputation: 726479
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.
Upvotes: 7
Reputation: 73366
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
Upvotes: 2
Reputation: 1099
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.
Upvotes: 1