Hi All,
I was to trying understand an source file ,I came across following line
Why is pParam typecasted to double char pointer?Code:void *pParam;
*((char**)pParam) = "SCHD_MODULE";
Thanks
Printable View
Hi All,
I was to trying understand an source file ,I came across following line
Why is pParam typecasted to double char pointer?Code:void *pParam;
*((char**)pParam) = "SCHD_MODULE";
Thanks
I'm thinking that void is already a pointer (NULL pointer), so *pParam would be a double pointer. Casted to char** since it's a char array (string) and then dereferenced once to give a char* pointer.
Equivalent:
Code:char *pParam = NULL;
*pParam = "SCHD_MODULE"; // read only
I'm not sure what you're trying to say here, but it's wrong. A void pointer isn't a NULL pointer, it's also not initialized to NULL. I don't know where you got NULL from, but there was nothing set to NULL in this example. It's a generic pointer, not a NULL pointer.
Quzah.
I still didn't understand the need for typecasting it to double pointer....
I still didn't understand the need for typecasting it to double pointer....
Let's define out custom type:
typedef char mytype_t,;
Now we want to pass that variable to a function to modify it. In order to do that, we need to pass it by address, right?
So,
mtype_t mydata;
foo(&mydata);
And
Easy. Right?Code:void foo(mytype_t * data)
{
*data = 'H';
}
Now let's redefine our type a little:
typedef const char* mytype_t;
And we once again pass it to a function:
mtype_t mydata;
foo(&mydata);
And
No problems! We've seen this stuff before.Code:void foo(mytype_t * data)
{
*data = "Hello World!"; // We probably should have malloc or something here, but this serves the example fine.
}
Now let's substitute mydata_t for the type it's an alias for, and we get:
const char * mydata;
foo(&mydata);
And
There you go. A double pointer.Code:void foo(char const* * data)
{
*data = "Hello World!"; // We probably should have malloc or something here, but this serves the example fine.
}
The reason for the casting is probably because the function takes an opaque pointer (ie void*). Thus it needs to convert it to its real type before doing something with it.