Sorry it must be very simple question , but since I tried in diffrenet ways without any success I have to ask here to be sure. C programming :
There is a struct name rtg
. EDIT: type of mtch is LLIST type of initial is LL_NODE typr of obj is pointer (void)
. Using gdb when I check
(gdb) print *rtg->mtch->initial->obj
Attempt to dereference a generic pointer.
(gdb) print rtg->mtch->initial->obj
$10 = (void *) 0x4cc660
(gdb) x 0x4cc660
0x4cc660: 0x00000000
This null pointer causes segfault in my program. What I am looking for is simply how to check the value of what rtg->mtch->initial->obj is pointing not be zero? (to prevent above segfault) I mean if I check with if (rtg->mtch->initial->obj)
, it would just check if pointer obj , adress not be zero (this is not what I intend , I intend to check the value of that pointer not be zero (but when I use * before checking in gdb it says "Attempt to dereference a generic pointer". So what is the correct way to check that value not be zero (and prevent this segfault)?
Edit : i had tried this
if (*((char *) rtg->mtch->initial->obj) != NULL)
but i got compile warning :
warning: comparison between pointer and integer
EDIT2 , here what are these defined in the source code
ECM_REQUEST is struct ECM_REQUEST rtg; in this struct defind mtch as LLIST mtch;
initial is LL_NODE
obj is a pointer i want to check obj value not be zero
so now everything are clear about my question isn't it?
Thanks
You cannot dereference a generic pointer. The only solution I can think of is to make a temporary pointer to integer to check the value. Basically
int *tmp = rtg->mtch->initial->obj;
if (*tmp != 0)
/* the rest of your code here */
A cast could also work, but having a temporary pointer makes the code easier to read in my opinion.
The warning is because the left hand side is a dereferenced char *
(in other words, a char
) and the right hand side is a null pointer constant, in the form of a void *
; You're converting a char
value to a void *
value... Perhaps you meant char **
?
void *o = rtg == NULL ? NULL
: rtg->mtch == NULL ? NULL
: rtg->mtch->initial == NULL ? NULL
: rtg->mtch->initial->obj;
if (o != NULL) { /* ... */ }
... Do note that it should be safe to change the type of o to any other object pointer (eg. your question seems to suggest char **
?)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.