This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed 7 years ago.
I'm working on learning pointers in C++, and was doing fine with int*, double*, etc. but then I tried something that I can't understand. I've attached my code segment, and the comments are the terminal output.
char word[] = "Hello there";
cout << word << endl; //Hello there
cout << *word << endl; //H
cout << &word << endl; //Address
char *wordpt = word;
cout << wordpt << endl; //Hello there
cout << *wordpt << endl; //H
cout << &wordpt << endl; //Address
wordpt = &word[0];
cout << wordpt << endl; //Hello there
cout << *wordpt << endl; //H
cout << &wordpt << endl; //Address
What's going on in these? I don't even understand how the contents of word (*word) can be a single index. How is word stored in memory? And why would wordpt allow me to give it a value of word, which isn't an address?
The pointer wordpt in the first case points to the first element of the array word, the second assignment is exactly the same thing but explicitly.
Arrays are automatically converted to pointers to their first elements as is specified by the c standard which also applies to c++.
What is confusing you is the fact that cout automatically prints the contents of a char * pointer instead of the address the pointer points to, to do it there is a requirement, the pointed to data must be a c string, i.e. a sequence of non-nul bytes followed by a nul byte, which is what is stored in word.
So cout is accessing the data pointed to by the wordpt pointer.
Related
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
I'm new to C++ and is trying to learn the concept of pointer. I have declared a pointer *pStart and initialised it to equal to 'text'. When I tried to print out the value of the pointer, I was expecting it to equal to the address to which 'text' was stored in, instead, the compiler printed out the string 'hello'. Could someone please explain it to me why this is happening?
char text[] = "hello";
char *pStart = text;
cout << pStart << " + " << *pStart << " + " << &pStart << endl;
This C++ feature is called operator overloading. Depending on the type of the input value certain type of streaming operator is invoked. For the character input look for "insert characters" at the link above or here.
Try this:
std::cout << std::hex << reinterpret_cast<int>(pStart);
I'm new to C++ and is trying to learn the concept of pointer. When I tried to print out the value of pStart, I was expecting its value to be the address of text[0] in hexdecimal (e.g. something like 0x7fff509c5a88). However, the actual value printed out is abcdef.
Could someone explain it to me why this is the case? What parts am I missing?
char text[] = "abcdef";
char *pStart = &text[0];
cout << "value of pStart: " << pStart << endl;
Iostreams provide an overload that assumes a pointer to char points to a NUL-terminated (C-style) string, and prints out the string it points to.
To get the address itself to print out, cast it to a pointer to void instead:
cout << "value of pStsart: " << (void *)pStart << "\n";
Note that you don't really need pStart here at all though. The name of an array (usually, including this case) evaluates to the address of the beginning of the array, so you can just print it directly:
cout << "address of text: " << (void *)text << "\n";
Get out of the habit of using endl as well. It does things you almost certainly don't realize and almost never want.
This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 7 years ago.
Perhaps a stupid question. When I cout the pointer to the char array, I thought it would print an address; instead it dereferences the address and prints the actual values till null.
As opposed to an int array where it does what I expect it to. It prints the address of the first element.
Why does the char element gets dereferenced when you print the pointer.
char* as = new char[100];
as[0] = 'a';
as[1] = 'b';
as[2] = NULL;
cout << as << endl;
int* s = new int[100];
s[0] = 2;
cout << s << endl;
Asking this because when I try to get the address to the first char element a[0] = 'a';. I have to store it in a pointer to a pointer. Which seems weird to me but that's besides the point.
char ** d = &as;
cout << d << "this is d" << endl;
There is no overloaded output operator << that prints the address for any char pointer, it treats all char pointers as strings. If you want to print the address of a pointer, you need to cast it to void*
std::cout << "Address of string is " << static_cast<void*>(as) << '\n';
On a side-note, the code
char ** d = &as;
cout << d << "this is d" << endl;
will not print the address of the string, i.e. the pointer contained inside as, instead it will print where the variable as is stored in memory. Not quite the same thing.
char* character pointers are considered to be C-style null terminated strings by the std::ostream << operator. You are right that this is a different behavior from other pointer types.
&a is not a pointer to a[0] . It is a pointer to a which is itself a pointer. a is in fact the pointer to a[0] and is equivalent to &a[0].
IOStreams treat char* (and const char*) specially, so that you can print C-strings without further effort:
std::cout << "hello world\n";
(Bear in mind that the string literal expression decays immediately to a const char* when passed to operator<<.)
If you do not want this behaviour, you can cast to void*:
char* as = new char[100];
as[0] = 'a';
as[1] = 'b';
as[2] = NULL;
cout << (void*)as << endl;
Your "fix" is actually broken, because you are printing the address of the pointer as, not the address of the array elements that as points to. This is indicated by the char** type, which you already noticed.
It prints the string because that's what the definition of that particular operator<< overload does. Cast to void * if you want to print an address:
cout << static_cast<void *>(as) << endl;
The << operator is overloaded to take a char* and output its contents where as there is no such overload for a int*/int[].
I have a function with three parameters: a pointer to a character array (also known as a C-String), and two pointers to specific characters (we will assume that they point to characters in the C-String).
void stringPointerOperation(char* str, char* firstPtr, char* secondPtr)
{
cout << str << endl;
cout << "First character=" << *firstPtr << endl;
cout << "Second character =" << *secondPtr << endl;
}
Questions:
How do I print out the characters from firstPtr to the end of str?
How do I find out how many characters are between firstPtr and secondPtr?
Answer to question 1:
If your array of chars is properly formatted, it should be null-terminated (i.e., the last character should be \0). Simply print characters until you get there, as:
while(*firstPtr != '\0') {
cout << *firstPtr << endl;
*firstPtr++;
}
Answer to question 2:
If you are sure they are pointers to the same array of characters, simply subtracting them should work:
int charsBetween = secondPtr - firstPtr;
I am doing some exercises to figure out how to access values in an array after they are changed with pointers. Can someone point out why the first output does not show the desired output? I am trying to get both cout to print 1234, one by using the new pointer and one by using the position in the array
int main()
{
char myArray[50]={0};
short* sizeOfAlloc=(short*)(myArray+5);
*sizeOfAlloc=1234;
cout << (short*)(myArray+5) <<endl;
cout << *sizeOfAlloc <<endl;
system("pause");
}
cout << (short*)(myArray+5) <<endl;
Prints the pointer. Not the value pointed by it.
cout << *((short*)(myArray+5)) <<endl;
^^ ^^
Will print the value pointed to by (short*)(myArray+5)