Why Is Address of Char Data Not Displayed

Why is address of char data not displayed?

When you are taking the address of b, you get char *. operator<< interprets that as a C string, and tries to print a character sequence instead of its address.

try cout << "address of char :" << (void *) &b << endl instead.

[EDIT] Like Tomek commented, a more proper cast to use in this case is static_cast, which is a safer alternative. Here is a version that uses it instead of the C-style cast:

cout << "address of char   :" << static_cast<void *>(&b) << endl;

Why C++ would not print the memory address of a char but will print int or bool? [duplicate]

I suspect that the overloaded-to-char * version of ostream::operator<< expects a NUL-terminated C string - and you're passing it only the address of one character, so what you have here is undefined behavior. You should cast the address to a void * to make it print what you expect:

cout<<"Address of e:"<< static_cast<void *>(&e) <<endl;

displaying address of char variable in c++ using pointers?

The only reason that you need to write the cast is because std::ostream has a special overload for char * which assumes that the given pointer points to the first element of a zero-terminated array of characters and will happily try to traverse that array until it hits zero.

Unfortunately, that means that you cannot simply pass the address of a single character and have the value of that address printed, which is why you need the cast. For all other (non-character) pointers, std::ostream already prints a numeric representation of the pointer value.

There isn't anything you can "do", but also there isn't really a problem, since you've already discovered the solution. If you are in a generic setting and want to print object pointers, then just add a cast to void * and it will work for all input types. The behaviour of std::ostream is the result of a design choice for convenience to make the vast majority of use cases work easily, at the expense of making your particular use case more verbose to write.

How to get address of C++ Char Datatype

To stuff something into an output-stream (std::ostream) like std::cout, the stream insertion operator << is used:

std::cout << "Hello, World!";

The stream insertion operator that is called for string literals like "Hello, World" looks like

std::ostream& operator<<(std::ostream& os, const char* s);

As you can see, the 2nd parameter is a pointer to const char. Now if you would write

char givenChar;
std::cout << &givenChar;

the address-of operator & would give you the address of givenChar. The type of this address is char* which is convertible into a char const*. So the above mentioned function

std::ostream& operator<<(std::ostream& os, const char* s);

would be called (like operator<<(std::cout, &givenChar)) which would interpret the memory at the location of the address of givenChar as a zero-terminated string. Eg. it would read from the memory until it finds a '\0'. But at the address of givenChar is only space for *one* char which most likely is not zero. This would result in garbage inserted into std::cout (=printed) and eventually lead to an access violation.

So instead you use

char givenChar;
std::cout << (void*) &givenChar;

(void*) is a cast. It converts the char* produced by applying the address-of operator & to the char givenChar into a pointer to void. For a void* the operator

ostream& operator<<(void* val);

gets called which will only insert the numeric value of the given address into the stream instead of trying to print a string that might exist at the address.

why an array reference variable cant hold address for char array in java?

For most objects, if you pass them to println, you get the normal toString() representation of the object. For arrays, it looks something like [C@6d4b1c02.

However, there is a version of println written specifically to accept a char array. So if you call that, you don't get the toString() representation of the array; you get the contents of the array, in this case ABC.

If you were to call the ordinary (non-char[]) version of println

System.out.println((Object) ch);

you would get the impenetrable [C@6d4b1c02 output.

char* doesn't hold the memory address [duplicate]

char* does in fact store an address. When you assign a constant string, C++ puts that constant string somewhere convenient (probably in the .DATA section of the executable) and stores a pointer to it in the variable x. In particular, this is one of the reasons a constant character string is, well, const. The string of text that the const char* points to may be used in other places in the code.

Note that cout has special behavior when printing character arrays. When you do &x, you're actually getting the address of the local variable, i.e. a thing of type const char**. If you want the actual address of the string, you can use (void*)x to suppress the usual string printing behavior.

How to print the address of char array

You need to cast to void* to invoke correct overload of operator << instead of outputting as C strings

std::cout << static_cast<void*>(c) << std::endl;
std::cout << static_cast<void*>(c+3) << std::endl;

Why does printing reference of a char type variable returns the value in it? [duplicate]

Its because of overload for operator<< (const char*) for ostream.

&c is of type char* and the operator<< tries to print 0-terminated C-string.
If you cast to void* as (void *)(&c) then you will get memory address as expected:

#include <iostream>

int main()
{
char c = 50;
int i = 50;

std::cout<<&c<<"\n"; // this will try to print null-terminated char* string.
std::cout << (void*)(&c) << "\n"; // this will print memory address as expected
std::cout<<*(&c)<<"\n";
std::cout<<&i<<"\n";

return 0;
}

Note: cout << &c may print some more characters, depending what bytes are present behind memory address &c ( the 0-byte could be far behind). So this is basically undefined behavior (UB).



Related Topics



Leave a reply



Submit