954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help to print address of char*

Hi,
Suppose I have a char*, which holds a string value.
Now I want to print the address of that char*, not the string value.
Pls help me in this problem.

Amit

amt_muk
Light Poster
48 posts since May 2005
Reputation Points: 14
Solved Threads: 3
 

Perhaps:

char * str = "something";
   std::cout << &str;
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Perhaps:

char * str = "something";
   std::cout << &str;

That would work, but here's a question for ya...

What about individual characters? Is it possible to get the address of those, or are char values stored in a special way?

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

>>What about individual characters? Is it possible to get the address of those,

What do you mean? Do you want to get the address of a char variable when it's declared as a straightforward, stand alone variable, when it's an element of a char array, when it's an individual char as part of a string, or when it's an element of a string whose address is stored in a pointer and you want to access it by dereferencing the pointer?

>>or are char values stored in a special way?

Not that I know of.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

>>What about individual characters? Is it possible to get the address of those,

What do you mean? Do you want to get the address of a char variable when it's declared as a straightforward, stand alone variable, when it's an element of a char array, when it's an individual char as part of a string, or when it's an element of a string whose address is stored in a pointer and you want to access it by dereferencing the pointer?

>>or are char values stored in a special way?

Not that I know of.

Then again I suppose it's not hard...

const char *c_string = "c-string";

const char& refChar = c_string[0];

std::cout << &refChar << std::endl;


...though I'm not near a C or C++ compiler at the moment so I can't confirm if this will work or not @_@

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Then again I suppose it's not hard...

const char *c_string = "c-string";

const char& refChar = c_string[0];

std::cout << &refChar << std::endl;

...though I'm not near a C or C++ compiler at the moment so I can't confirm if this will work or not @_@


Or even easier :)

std::cout << reinterpret_cast<void*>( c_string );


edit: that will only work if c_string isn't a const.

edit 2: in order to make that work with c_string as a const, change the cast like this:

std::cout << reinterpret_cast<const void*>( c_string );

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

Perhaps:

char * str = "something";
   std::cout << &str;

Can u pls explain the o/p for the following,

char* str = 0;
std::cout<<&str<<std::endl;


Output : 0012FF24

Amit

amt_muk
Light Poster
48 posts since May 2005
Reputation Points: 14
Solved Threads: 3
 

Can u pls explain the o/p for the following,

char* str = 0;
std::cout<<<strong>&str</strong><<std::endl;

Output : 0012FF24

Amit

Most likely because--
// *str is equivalent to... I don't know, most likely undefined behavior so don't dereference @_@

// str is equivalent to NULL because all pointers can point to the NULL address which is represented by 0

// &str is most likely the address of the actual pointer O_O

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 
Suppose I have a char*, which holds a string value.


char* generally does not hold a string value, but a pointer to a character (maybe a pointer to the first character in a string of characters)Now I want to print the address of that char*, not the string value.
1) The address of the char*, meaning the address of the pointer is displayed by:

char *st = "hello";
cout << (int)&st << endl;


2) The address of the string "hello" is displayed by:

char *st = "hello";
cout << (int)st << endl;
char* str = 0;
std::cout<<<strong>&str</strong><<std::endl;


this displays the address of the pointer, as above in 1). If you wanted to see assigned 0, then you would display the value of the char*, as above in 2).What about individual characters? Is it possible to get the address of those,
some examples of displaying the address of some characters:

char a, b, c;
char *st = "hello";

cout << (int)&a << endl;   // display the address of a
cout << (int)&st[1] << endl;   // display the address of the 'e' in "hello"
dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

char* generally does not hold a string value, but a pointer to a character (maybe a pointer to the first character in a string of characters)

1) The address of the char*, meaning the address of the pointer is displayed by:

char *st = "hello";
cout << (int)&st << endl;

2) The address of the string "hello" is displayed by:

char *st = "hello";
cout << (int)st << endl;

this displays the address of the pointer, as above in 1). If you wanted to see assigned 0, then you would display the value of the char*, as above in 2).


got it.

Thanx a lot.

Amit

amt_muk
Light Poster
48 posts since May 2005
Reputation Points: 14
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You