Printing the address of char variable

Sir as in the lecture you said that when we try to print the address of a character variable, the character itself will be displayed (without doing the explicit typecasting), when i tried to print the character itself using the & operator it is neither displaying the character nor its address ( it is displaying me ( aff&@ ) which is not even a hexadecimal code. It is showing me correct results after typecasting( i.e., is address is being displayed ). So, why i am getting that code and what is it?

Hello @lakshaysinghal08,

Please, share your code.

1 Like

#include
using namespace std;
int main()
{
int a;
float f;
char c=‘a’;
cin>>a>>f;
cout<<a<<endl<<f<<endl;
cout<<&a<<endl<<&f<<endl<<&c<<endl;

return 0;

}
// I was using this code I to print the character itself but this code was printing some other random code which is neither the character nor its address, it is not even a hexadecimal code.
I am attaching the screenshot of its output.
image.png
I want to know why this random code is being printed and what is it??
// I am getting correct results after typecasting the address of character variable.

Hello @lakshaysinghal08,

  1. There is no SS attached with your reply.

  2. It was explained in one of the tutorials that there is an exception,
    when you have to print the address of character, then & (Address of) operator fails to do so.
    For that, you have to typecast it into void *
    cout<<(void*)&c; will produce the desired address.

    So, what is the gibberish output?
    if you observe that carefully, the first character of that is ‘a’ which is the value stored in c, followed by some random characters.

    What are those characters?
    When you print &c, the compiler keeps on printing the characters stored at contiguous memory locations starting from the address of the variable c until it encounters ‘\o’ (Null Character).
    Same is happening in your code.
    after printing value of c i.e. ‘a’ at location 1(say)
    it prints:
    f at 2
    f at 3
    & at 4
    @ at 5
    and then at 6, there is a null character, so the output terminates.

Hope, this would help.
Give alike, if you are satisfied.

But sir as we are passing only the address of char c, it should only print the character stored in c, why it is printing the characters stored in next contiguous memory locations.
I tried it several times, sometimes it is printing some gibberish output and sometimes only the character stored in that variable, I am also mentioning a code where half of the times it is printing some gibberish code and then it is printing only the character stored in that variable.
code:-
#include
using namespace std;
int main()
{
int a;
float f;
char c=‘a’;
char d=‘e’;
char j=‘r’;
char g=‘w’;
cin>>a>>f;
cout<<a<<endl<<f<<endl;
cout<<&a<<endl<<&f<<endl<<&c<<endl;
cout<<&d<<endl<<j<<endl<<g<<endl;

return 0;

}
Screenshot-

Hey @lakshaysinghal08,

  1. Try to run the following modified code:

#include
using namespace std;
int main()
{
int a;
float f;
char c=‘a’;
char d=‘e’;
char j=‘r’;
char g=‘w’;
cin>>a>>f;
cout<<a<<endl<<f<<endl;
cout<<&a<<endl<<&f<<endl<<&c<<endl;
cout<<&d<<endl<<j<<endl<<g<<endl;

//Memory location of c and d;
cout<<(void *)&c<<endl<<(void *)&d;

//You would realize that variable d has an address one bit before c.
//This is the reason the output &c is a part of &d.
//But, they both terminate after the same character on encountering ‘\0’ (Null Character)
//g and j are printing their values because the next character in the memory location is ‘\0’.

return 0;
}

  1. Watch the following video for better understanding:
    CPP - Address Of Operator ‘&’ under Pointers and functions

Hope, this would help.

Sir in the video it is told that due to " << " operator it will only print the character stored in that variable, so my question was then why the values stored in contiguous memory locations are getting printed as we are not passing their address then you told me that the printing will stop only when it will encounter a null character. So, there should always be a null character stored in a nearby memory location as if it is not there it will print a long gibberish code, how that thing is maintained in memory? as sometimes we are getting the next value as a null character and sometimes we are getting null character after 6-8 memory locations??
And secondly when i tried to run my code several times with multiple changes i observed that it is allocation the memory unit on a random basis, sometimes it is allocating contiguous memory locations to all the variables and sometimes it is allocating far memory locations, So i want to know that how the memory units are allocated to a variable? Is there any pattern for memory allocation??

hello dear sir, i want to know how to print the address of a character variable.