NULL指针和零值 [英] NULL pointer and zero value

查看:97
本文介绍了NULL指针和零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个指针char * p,通过以下方式为此

指针指定NULL是否正确:


" memset(& p ,0,sizeof(p));而不是p = NULL;


我问的原因是我有一个N函数结构数组

变量,例如,


typedef struct

{

int(* func1)();

int(* func2 )();

void *(* func2)(int);

} ModuleFunctions;


#define N 100

ModuleFunction garMF [N];


初始化时,不要为每个

函数成员构造一个循环来分配NULL对于每个数组成员的ModuleFunction,是

它是正确的,例如


memset(garMF,0,sizeof(ModuleFunctions)* N);

感谢您的帮助,


DT

If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"

The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like

memset( garMF, 0, sizeof(ModuleFunctions)*N );
Thanks for your help,

DT

推荐答案

vp写道:
vp wrote:
如果我有一个指针char * p,通过以下方式为这个
指针指定NULL是否正确:

" memset(& p, 0,sizeof(p));而不是p = NULL;
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"




编号NULL不一定是全位零。


但是,你的技术会在很多平台上偶然发挥作用,因为很多平台都会使用all-bits-zero来表示NULL。如果你喜欢

编程一个赌场,请继续。 :-)


< snip>


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方。 ; - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton



No. NULL is not necessarily all-bits-zero.

Your technique will, however, work by chance on quite a few platforms,
because quite a few platforms do use all-bits-zero for NULL. If you like
programming-by-casino, go ahead. :-)

<snip>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Richard Heathfield< in ***** @ address.co.uk.invalid>这样说:
Richard Heathfield <in*****@address.co.uk.invalid> spoke thus:
(所有位0为空的帖子)
(all-bits 0 as NULL post)


如果你喜欢按赌场编程,请去先。 : - )


If you like programming-by-casino, go ahead. :-)




所以房子赢得NULL和双NULL? ;)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



So the house wins on NULL and double NULL? ;)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


dt ******* @ yahoo。 com (vp)写道:
dt*******@yahoo.com (vp) writes:
如果我有一个指针char * p,通过以下方式为此指针指定NULL是否正确:

" memset(& p,0,sizeof(p));"而不是p = NULL;


编号前一个语句将'p''的所有位设置为零,这不是

必然是空指针的表示。

我问的原因是我有一个N函数
变量的结构数组,例如,

typedef struct
{
int(* func1)();
int(* func2)();
void *(* func2)(int);
} ModuleFunctions;

#define N 100
ModuleFunction garMF [N];

初始化时,不是为每个数组成员的struct ModuleFunction的每个
函数成员设置一个循环,而是
正确做一些像memset(garMF,0,sizeof(ModuleFunctions)* N);
If I have a pointer char * p, is it correct to assign NULL to this
pointer by:

"memset( &p, 0, sizeof(p));" instead of "p = NULL;"
No. The former statement sets all bits of `p'' to zero, which is not
necessarily a representation of the null pointer.
The reason I ask is I have an array of structure of N function
variables, for example,

typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions;

#define N 100
ModuleFunction garMF[N];

When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like

memset( garMF, 0, sizeof(ModuleFunctions)*N );




如果`garMF''有静态存储持续时间,如果你没有明确地初始化它,它会自动初始化为
零。 初始化为零意味着每个非复合对象被初始化,就好像你给它分配了'0''/ b $ b(即指针被初始化为空指针)。对于复合对象,

其成员或元素以这种方式递归初始化。


否则(如果`garMF''没有静态存储持续时间)你将循环使用




马丁



If `garMF'' has static storage duration, it is automatically initialized to
zero if you don''t initialize it explicitly. "Initialized to zero" means
that each non-compound object is initialized as if you assigned `0'' to it
(i.e. pointers are initialized to null pointers). For compound objects,
its members or elements are recursively initialized in this way.

Otherwise (if `garMF'' doesn''t have static storage duration) you''ll have
to loop.

Martin


这篇关于NULL指针和零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆