+ 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; }

27th May 2023, 5:07 PM
Lucas Sicilia MartĂ­nez
Lucas Sicilia MartĂ­nez - avatar
13 Answers
+ 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.
27th May 2023, 6:21 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 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)
27th May 2023, 5:38 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 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
28th May 2023, 5:56 AM
Emerson Prado
Emerson Prado - avatar
+ 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.
27th May 2023, 5:43 PM
Orin Cook
Orin Cook - avatar
+ 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.
28th May 2023, 12:19 PM
Ipang
+ 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; }
27th May 2023, 6:04 PM
Lucas Sicilia MartĂ­nez
Lucas Sicilia MartĂ­nez - avatar
+ 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 *.
27th May 2023, 6:26 PM
Orin Cook
Orin Cook - avatar
+ 1
Orin Cook yeah good point it'll give an error but still char* will output the value of the variable not address
27th May 2023, 6:28 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 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
28th May 2023, 1:05 AM
Bob_Li
Bob_Li - avatar
+ 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
28th May 2023, 1:38 PM
Lucas Sicilia MartĂ­nez
Lucas Sicilia MartĂ­nez - avatar
0
Share your code
27th May 2023, 5:35 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Yeah, my problem was that I didn't expect it to actually work correctly with int* without typecasting lol
27th May 2023, 6:32 PM
Orin Cook
Orin Cook - avatar
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.
27th May 2023, 6:35 PM
Orin Cook
Orin Cook - avatar