[英]how to check the value of pointer (void) not be zero (Attempt to dereference a generic pointer)
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. 抱歉,这必须是一个非常简单的问题,但是由于我以diffrenet的方式尝试了但没有成功,因此我必须在这里进行确认。 C programming :
C语言编程
There is a struct name rtg
. 有一个结构名称
rtg
。 EDIT: type of mtch is LLIST type of initial is LL_NODE typr of obj is pointer (void) 编辑:mtch的类型是LLIST的类型的初始是LL_NODE obj的类型是指针(无效)
. 。 Using gdb when I check
我检查时使用gdb
(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?
我在寻找的只是简单地检查rtg-> mtch-> initial-> obj所指向的值是否不为零? (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)? (以防止上面的段错误)我的意思是,如果我检查
if (rtg->mtch->initial->obj)
,它将只是检查指针obj,地址是否为零(这不是我想要的,我打算检查该指针的值不为零(但是在检入gdb之前我使用*时,它说“试图取消引用通用指针”。那么检查该值不为零的正确方法是什么(并防止此段错误)?
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 EDIT2,这是在源代码中定义的内容
ECM_REQUEST is struct ECM_REQUEST rtg; ECM_REQUEST是struct ECM_REQUEST rtg; in this struct defind mtch as LLIST mtch;
在此结构中,将mtch定义为LLIST mtch;
initial is LL_NODE 首字母为LL_NODE
obj is a pointer i want to check obj value not be zero obj是我要检查obj值不为零的指针
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 *
; 警告是因为左侧是已取消引用的
char *
(换句话说,为char
),而右侧是空指针常量,形式为void *
; You're converting a char
value to a void *
value... Perhaps you meant char **
? 您要将
char
值转换为void *
值...也许您是说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 **
?) ...请注意,将o的类型更改为任何其他对象指针应该是安全的(例如,您的问题似乎建议使用
char **
吗?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.