0

i was reading a book and came across this line
"C++ matches a pointer of any other type with type void * and prints a numeric representation
of the address. If you want the address of the string, you have to type cast it to
another type,"
please any one could explain this with the help of an example.

2
Contributors
1
Reply
8
Views
2 Years
Discussion Span
Last Post by Ancient Dragon
0

void* is just a generic term for a pointer of any type. For example, malloc() returns void*. If you really want a pointer of type char* then, in c++, you have to typecast the return value of malloc().

char* p = (char*)malloc(15);

In the above, malloc will allocate memory for 15 bytes of memory and return a pointer to that memory block. Since pointers are all the same size in c++ you can easily just typecast it to whatever kind of pointer you want.

Edited by Ancient Dragon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.