+ 2

What are (void*) and static_cast<void*> ?

i just learn pointer in C++ course. trying to get some memory address of variable. but when i want to get memory address of char, i have to use (void*) and static_cast<void*>. what are they? why i need them to get memory address of char? and the most important can i use them for other purpose? like .... i don't know. please give some simple explanation. https://code.sololearn.com/crPAuWOTZgLt/?ref=app

13th May 2017, 2:02 PM
Setiawan Next
Setiawan Next - avatar
6 Answers
+ 3
@Setiawan Next, I used f because it lets the compiler know that 10.3 is a float number. By default 10.3 is a double unless you specify it as a float using f at the end of it (but you dont have to do that, your compiler will most likely see that you are assigning it to float and wont have to do a cast) You can read a little about the difference between thise casts here: http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast
13th May 2017, 2:39 PM
aklex
aklex - avatar
+ 4
I don't think that f is needed. Only mentioning the data type will suffice. Ya you can use that (int) type casting as shown in the example.
13th May 2017, 2:30 PM
Vishnu ks
Vishnu ks - avatar
+ 3
Those are casts into a void pointer. You dont need them to get the address of a char though. You can do that with the & operator instead, like so: void* p = &myVar; An example of a cast: Float a = 10.3f; Int b = static_cast<int>(a); Which converts a into an int type
13th May 2017, 2:05 PM
aklex
aklex - avatar
+ 3
Ya aklex is right
13th May 2017, 2:08 PM
Vishnu ks
Vishnu ks - avatar
+ 3
thanks @Vishnu ks
13th May 2017, 2:31 PM
Setiawan Next
Setiawan Next - avatar
+ 2
thanks @aklex can i ask a question again? why you add f in the 10.3f ? and can i use (int) to convert instead of static_cast<> like : int b = (int)a;
13th May 2017, 2:25 PM
Setiawan Next
Setiawan Next - avatar