+ 1
&[char variable] doesn't show a memory position
It is different from other &[any type of variable] function I share the (basic) code: #include <iostream> #include <string> using namespace std; int main() { int i1 = 1; int *intPtr; intPtr = &i1; cout << intPtr << endl; printf ("It works"); char c1 = 'a'; int *charPtr; charPtr = &c1; cout << charPtr << endl; printf ("It doesn't work"); str s1 = "stringexample"; int *stringPtr; stringPtr = &s1; cout << stringPtr << endl; printf ("It doesn't work either"); return 0; }
13 Antworten
+ 5
Orin Cook Lucas Sicilia MartĂnez
In c++, when printing a char* pointer using std::cout, the behavior is different. std::cout interprets char* pointers as C-style strings by default and attempts to print the string value instead of treating them as regular pointers. To display the memory address of a char variable, you can cast it to void* explicitly.
while
int* pointers are directly interpreted as memory addresses and prints them without requiring any explicit type casting.
+ 3
I think you are with c++
to print memory address of character variable using the & operator, you can use a type cast to void* to correctly interpret the address.
like:
static_cast<void*>(&variable)
+ 3
Lucas Sicilia MartĂnez One very important point: in programming, "doesn't work" is no information. Pls tell what happens. Do you get an error message? Which one, exactly? Or do you get a wrong answer? If so, describe what makes it wrong.
Debugging starts with a clear understanding of what went wrong.
Plus, here goes the way to share your code in a way people can actually test and debug it, and avoids any copy errors:
1. Save your code in Code Playground as public
2. Edit your question description
3. Tap "+" button, then "Code"
4. Select your code from Code Playground
+ 2
Snehil's answer is an important point, but what concerns me here is your assertion that "it is different from other &" types, because that's definitely not true even accounting for any difficulties printing pointers correctly. Those should be the same for int etc as well.
So, yeah, share your code because you're doing something weird.
+ 2
Snehil is correct,
You may find more about the "reason why", and recommendations/workarounds in the following SO discussion
https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value
And I second Emerson Prado's sugggestions about link to a code bit. Because I see some problem in the snippet in post Description - apart from the reasoning for unexpected output:
int *charPtr;
....
int *stringPtr;
These two lines already enough to bring trouble, I'm sure you know what I mean. This issue would have been quickly identified had you attached a code bit link rather than raw text code.
And please, tag C++, the post deserved that tag for context clarity, and in future searchability.
+ 1
HERE IT IS: (I'll trySnehill advice)
#include <iostream>
using namespace std;
int main()
{
int i1 = 1;
int *intPtr;
intPtr = &i1;
cout << intPtr << endl;
printf ("It works");
char c1 = 'a';
int *charPtr;
charPtr = &c1;
cout << charPtr << endl;
printf ("It doesn't work");
return 0;
}
+ 1
Huh, interesting.
The bigger problem here with this code is with the pointers though, not anything that nuanced: chars and strings should get char* , not int *.
+ 1
Orin Cook yeah good point it'll give an error
but still char* will output the value of the variable not address
+ 1
Lucas Sicilia MartĂnez
Yes, cout interprets char* &char differently. You have to cast it to a void pointer to display the address with cout.
Here is a working example gathered from the suggestions given here.
https://code.sololearn.com/c9jrQajun8vq/?ref=app
+ 1
Thank you to all of you.
I'll share it as you suggested as soon as I can.
Future issues I'll do as you said.
Thanks
0
Share your code
0
Yeah, my problem was that I didn't expect it to actually work correctly with int* without typecasting lol
0
Oh btw, for OP to follow along successfully: the output issue that we're taking about is a characteristic of how cout handles different types, and is still not a difference in the pointers themselves.