pointers are confusing me HELP! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

pointers are confusing me HELP!

#include <stdio.h> void func(char * data){ printf("%s",data); } int main() { char payload[30] = "this is random data ...\n"; void * ptr; ptr = &payload; //HERE ! // i saw someone using (char *) before the name of the variable and when i tried it it gave me the same output as the function without it ... can someone tell me why he used it ? func(ptr); func((char *)ptr); return 0; } output: this is random data ... this is random data ...

16th Aug 2021, 2:20 AM
ox titanium
ox titanium - avatar
3 Answers
+ 2
So it just a implicit and explicit typecasting, void can type cast to any type
16th Aug 2021, 2:36 AM
Mehrdad Sh
Mehrdad Sh - avatar
+ 2
To conform with function signature which accepts char* argument probably. Had you defined <ptr> as char* rather than void*, there will be no need for explicit casting on function call.
16th Aug 2021, 3:00 AM
Ipang
+ 1
I don't know how to explain it to you, but I know something that can help. Thank me later ;) https://youtube.com/playlist?list=PL2_aWCzGMAwLZp6LMUKI3cc7pgGsasm2_
16th Aug 2021, 2:50 PM
Rishi
Rishi - avatar