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

Perhaps:

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

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?

>>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.

>>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 @_@

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 );
commented: Way too crafty XD +4

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

commented: Great questions! =) +4

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

char* str = 0;
std::cout<<[B]&str[/B]<<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

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<<[b]&str[/b]<<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"

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.