This question already has answers here:
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 7 years ago.
Why in my code I see value, which stored in the address, but not adress?
char *fortunes[] =
{
"1",
"2",
"3",
"4"
};
cout << *fortunes[2]; // result 3
cout << fortunes[2]; // result 3, but I expected to see adress
There is an overload for std::ostream& operator<< which takes a const char* and interprets it as a null-terminated string, printing out characters until it finds the null terminator. If you want to print the value of the pointer (the address it holds), you can cast it to void*.
For example
cout << reinterpret_cast<const void*>(fortunes[2]) << endl;
Related
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Why does cout print char arrays differently from other arrays?
(4 answers)
How does std::cout work with char pointers?
(1 answer)
Closed last month.
I can't understand why cout prints DATA for str variable. str does not contain the memory address of the first caracter ? What does it mean DATA ?
#include<bits/stdc++.h>
using namespace std;
int main()
{
char str[7]=”DATA”;
cout << str[2]<<” “<<str;
return 0;
}
It prints T DATA.
Thank you for your help,
operator<< has an overload for const char* that prints out characters until it hits a null terminating character. And a char[] array decays into a char* pointer to its 1st element.
So, when we print str[2] it just prints the single character T, but when we print str it prints all of the characters.
str[2] is a single char, T, which you see.
str, on the other hand, is a char* (read: pointer to char), and cout will print it as a c-string - i.e., print all the characters until it reaches the null character (\0), similar to what printf would have done. If you don't want that overload, you could cast it to a void*:
cout << str[2] << " " << (void* ) str;
This question already has answers here:
How to print the address of char array
(1 answer)
cout << with char* argument prints string, not pointer value
(6 answers)
Why does the index of a `char **` type give the whole string?
(2 answers)
Closed 2 years ago.
In C++ could someone clarify
1- In he following code, printing char* is displayed as bunch of random characters, but printing string* is just an integer address? Why so?
int * intptr;
char * charptr;
std::string * stringptr;
float * floatptr;
//printing
std::cout << intptr << " , " << charptr << " , " << stringptr << " , " << floatptr << "\n";
output:
0x7ffeea2f1a60 , ���� , 0x7ffeea2f1a48 , 0x1092d13d4
2- charptr is a pointer variable painting to part of memory that contains a character. Just for curiosity, how do you print the address of charptr?
3- While reading different sources, I came across to this: "The different printing is because of the functions (i.e. a const char *) that treat that memory as a sequence of characters". Could someone expand this answer w.r.t the above code?
char* is special with regard to printing. The reason is the historical legacy of C, where strings are represented as arrays of characters. So most of the time given a char* what you want to do is print the characters it points at.
I don't think you mean print the address of charptr (that would just be cout << &charptr), I think you mean print the address which is the value of charptr. The way to do that is to cast the address to void* before printing it.
cout << static_cast<void*>(charptr);
Your third question is essentially what I explained in the first paragraph above.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
For example, i have the following string :
std::string s = "Hello, World!"
I want the address of the last element of s which is '!'.
I tried the following code, but it does not seem to output what I want it to.
std::cout << &s[s.length() - 1];
That outputs '!' not it's address, the same happens with s.back()
Is it because of the formatting caused by std::cout or is the problem elsewhere?
Basically I want a function that outputs the address of the last (and if possible, the first) element in a string.
Currently, you're using the overload of operator<< that takes a const char* as input. It treats the input as a null-terminated C string.
If you cast to (const void*) the problem will go away:
std::cout << (const void*)(&s[s.length() - 1]);
auto address_back = &s.back();
auto address_front = &s.front();
cout << static_cast<void*>(address_back) << endl;
cout << static_cast<void*>(address_front) << endl;
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);
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
For the code given below, why is the output "This is the string" instead of the address of the first character in the string, 'T'?
int main()
{
char myString[] = "This is a string";
char *ptr = &myString[0];
cout << ptr << endl;
return 0;
}
Output is to be clicked above.
why is the output "This is the string" instead of the address of the first character in the string, 'T'?
There is an operato<< overload whose LHS is a std::ostream and the RHS is char const*. This function prints the string.
If you want to print the address of 'T', you can cast the pointer to a void*.
cout << static_cast<void*>(ptr) << endl;
char *ptr = &myString[0];
Means make ptr point to the first character of myString. Thencout has an overload of << that takes a char * and will print what it points to and and the preceding elements until it reaches an '\0'
If you want to print the address of the array then you need to convert the pointer to something else like a void* first and then print:
cout << reinterpret_cast<void*>(ptr) << endl;