Addition: In c++ void fun(); and void fun(void); are same But in C Void fun(); means fun can be called with any no. Of arguments and void fun(void); means fun can be called only without argument.
fun() and fun(void) are different in c, but same in c++ In C fun() means any number of arguments, but in C++ fun() means no argument fun(void) means no argument in both C and C++
There are 2 things I have to say. 1) You CAN use const variables with void pointers. You just need to make the void* const as well. Meaning: const int* p = new int(20); const void* ptr = p; This will compile and work just fine. 2) Your point about function pointer is right, but the example is wrong You tried to cast "fun" which is a *function* into type "void*". A function is not a function pointer and if you wanted to show that you should have created an actual function pointer like this: void (*f_ptr)() = &fun; And then use this in main like void* ptr = static_cast(f_ptr); This also won't compile but to show function pointers can't be casted into void pointers, this is an appropriate example. HOW EVER, it is possible to cast even function pointers into void pointers using reinterpret_cast but, quoting: "It should always work for every compiler, but it's not always safe" Meaning depending on your compiler it could be undefined behavior. So don't do it. But in case if anyone wants to know, this is how. auto f = &fun; void* ptr = reinterpret_cast(f); Edit: I know this isn't the topic of the video but because of the function pointer issue + using any raw pointer (void* in this case) is hard and unsafe, just use std::any. STL implementations are most likely best you can go with and since most of them are constexpr and template based, optimizer will optimize your code much more than you can by hard coding it. Plus, it handles everything automatically what you normally have to handle with pointers. And it even works with rvalues unlike void*.
@@CppNuts Thank you! Also just realized I look like I was criticizing so much, I forgot to appreciate how good the video was. Thank you for the great content!
Dude, it doesn't matter if you criticized but the important point is you are looking forward to help which is very important for me. And really i didn't feel you criticized at all.
casting of const pointer is possible with const void. we are not loosing constness after cast so possible. const int *p = new int(10); const void *vp = static_cast(p);
With void pointers is arithmetic can not be performed in a void pointer. Example:- void *ptr; int a; ptr=&a; ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable.
Question: I do assign &var (7) to a *voidptr (void pointer). I do cast the void pointer to int pointer. Afterwards, I do print out *voidptr (gives me address of the variable) and **voidptr (gives me value of the variable). Why does not *voidptr dereference give me the value (7), but the address?
Hello Sir, Can you please re-arrange Playlist for interview questions, most of the videos are repeating in playlist, which makes it difficult to keep track :) I follow your channel from last 1 year.. You are awesome !
how can I put this void pointer into a class? void * elimina(void * s){ struct nodo *p,* aux; if(s==NULL) printf(" pila vacia "); else{ p=(struct nodo*)s; s=p->siguiente; free(p); } return(s); } Thanks!
Thank you CppNuts Team. Data about void* are crisp and clear.
Addition:
In c++ void fun(); and void fun(void); are same
But in C
Void fun(); means fun can be called with any no. Of arguments and void fun(void); means fun can be called only without argument.
fun() and fun(void) are different in c, but same in c++
In C fun() means any number of arguments, but in C++ fun() means no argument
fun(void) means no argument in both C and C++
There are 2 things I have to say.
1) You CAN use const variables with void pointers. You just need to make the void* const as well. Meaning:
const int* p = new int(20);
const void* ptr = p;
This will compile and work just fine.
2) Your point about function pointer is right, but the example is wrong
You tried to cast "fun" which is a *function* into type "void*". A function is not a function pointer and if you wanted to show that you should have created an actual function pointer like this:
void (*f_ptr)() = &fun;
And then use this in main like
void* ptr = static_cast(f_ptr);
This also won't compile but to show function pointers can't be casted into void pointers, this is an appropriate example.
HOW EVER, it is possible to cast even function pointers into void pointers using reinterpret_cast but, quoting:
"It should always work for every compiler, but it's not always safe"
Meaning depending on your compiler it could be undefined behavior. So don't do it.
But in case if anyone wants to know, this is how.
auto f = &fun;
void* ptr = reinterpret_cast(f);
Edit:
I know this isn't the topic of the video but because of the function pointer issue + using any raw pointer (void* in this case) is hard and unsafe, just use std::any. STL implementations are most likely best you can go with and since most of them are constexpr and template based, optimizer will optimize your code much more than you can by hard coding it.
Plus, it handles everything automatically what you normally have to handle with pointers. And it even works with rvalues unlike void*.
Thanks, it's really valuable comment and help someone seeking more info.
I appreciate it.
@@CppNuts Thank you! Also just realized I look like I was criticizing so much, I forgot to appreciate how good the video was. Thank you for the great content!
Dude, it doesn't matter if you criticized but the important point is you are looking forward to help which is very important for me. And really i didn't feel you criticized at all.
@@CppNuts I'm glad then :)
casting of const pointer is possible with const void. we are not loosing constness after cast so possible.
const int *p = new int(10);
const void *vp = static_cast(p);
With void pointers is arithmetic can not be performed in a void pointer.
Example:-
void *ptr;
int a;
ptr=&a;
ptr++; // This statement is invalid and will result in an error because 'ptr' is a void pointer variable.
Question: I do assign &var (7) to a *voidptr (void pointer). I do cast the void pointer to int pointer. Afterwards, I do print out *voidptr (gives me address of the variable) and **voidptr (gives me value of the variable). Why does not *voidptr dereference give me the value (7), but the address?
void pointer is basically HANDLE in the windows.h library. But why is a HANDLE useful? Why don't people just use a void* insteam of HANDLE?
void *p =f where f is declared as a pointer to a function is possible
Yes!!
Hello Sir, Can you please re-arrange Playlist for interview questions, most of the videos are repeating in playlist, which makes it difficult to keep track :) I follow your channel from last 1 year.. You are awesome !
you find the same video in different playlist?
or same video in same playlist?
@@CppNuts same video in different playlist
Can you plz tell which compiler you are using??
I was using linux OS name xubuntu, code editor is sublime text editor, and the you need comiler which is basic comes in linux.
Thank you for replying...Can you suggest any good compiler for windows OS.
You can google like "how to setup sublime with mingw in windows" And you will get it, better search in youTube it self.
Do you have a github repo sir?
Amazing. Thanks
Thanks man!!
good job :)
+Shiv Kumar Yadav Thanks dude.
@@CppNuts bro which IDE you are using In this video ?
It must be Sublime text editor
@@CppNuts is it free or i have to purchased ?
Free
Thanks a lot sir...
So nice of you
Sir, Sanjeev is a noobra number one 😁😁😁
Can anyone explain static_cast
I have a video, search in RUclips.
Thank u
Basically converts one type to another.
For example if you can convert a int to a double.
how can I put this void pointer into a class?
void * elimina(void * s){
struct nodo *p,* aux;
if(s==NULL)
printf("
pila vacia
");
else{
p=(struct nodo*)s;
s=p->siguiente;
free(p);
}
return(s);
}
Thanks!
awesome
tanks dude
Thanks man!!
ok
OK :D
ok :)