指针和结构变量之间有什么区别 [英] what difference between pointer and struct variable

查看:82
本文介绍了指针和结构变量之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



亲爱的,


我最近调试程序如下。


#include< sys /stat.h>


int main(int argc,char * argv [])

{

struct stat buf;


stat(argv [1],& buf);


...

返回0;

}


int main(int argc,char * argv [])

{

struct stat * buf;


stat(argv [1],buf);


...


返回0;


}


和他们有什么区别?

为什么我有修改这样的第二个程序:

{

struct stat temp,* buf =& temp;


stat(argv [1],buf);


...


}

非常感谢

解决方案

" J Wang" < CS **** @ bath.ac.uk>在留言中写道

新闻:Pi ************************************ ** @ amos .bath.ac.uk ...


亲爱的,

我最近调试程序如下。
#include< sys / stat.h>

int main(int argc,char * argv [])
{
struct stat buf;

stat(argv [1],& buf);

...
返回0;
}
int main(int argc ,char * argv [])
{
struct stat * buf;


这是一个指向类型''struct stat''对象的指针。

由于你没有初始化它或给它赋值,

这个指针的值是不确定的(它没有点b $ b点任何地方。)


stat(argv [1], BUF);


此语句将未知(即随机)指针

值传递给函数''stat()''。如果该函数尝试取消引用该指针,则该程序的行为将变为

undefined。


现在你需要一个''struct stat''指针指向

指向,但没有提供一个。

...

返回0;

}

与他们有什么不同?
为什么我要修改这样的第二个程序:
{
struct stat temp ,* buf =& temp;

stat(argv [1],buf);

...

}




因为你需要一个指针指向的对象。

指针定义不会自动创建一个

对象指向。那是你的工作。


-Mike


J Wang写道:

亲爱的,

我最近调试程序如下。

#include< sys / stat.h>

int main(int argc,char * argv [])
{struct stat buf;

stat(argv [1],& buf);

...
返回0;
}
int main(int argc,char * argv [])
{
struct stat * buf;

stat(argv [1],buf);

...

返回0;

}
与他们有什么不同?
为什么我要修改这样的第二个程序:
{struct / temp struct * * buf =& temp;

stat(argv [1],buf);

...

}

非常感谢




第一个例子很好,因为buf是一个结构对象。

秒不起作用,因为没有结构对象。第三个

有效,因为temp是一个对象,buf指向它。

-

Joe Wright mailto:jo ***** ***@comcast.net

一切都应该尽可能简单,但不能简单。

---阿尔伯特爱因斯坦---


On Sun,2004年7月18日14:24:03 GMT,Mike Wahler

< mk ****** @ mkwahler达网络>写道:

" J Wang" < CS **** @ bath.ac.uk>在消息中写道
新闻:Pi ************************************** @ amo s.bath.ac.uk ...


亲爱的,

我最近调试程序如下。

#include < sys / stat.h>

int main(int argc,char * argv [])
{
struct stat buf;

stat (argv [1],& buf);

...
返回0;
}
int main(int argc,char * argv [])
{struct struct * buf;
这是一个指向类型''struct stat''对象的指针。
因为你没有初始化它或指定它一个值,
这个指针的值是不确定的(它没有指向任何地方。)

stat(argv [1 ],buf);



此语句将未知(即随机)指针
值传递给函数''stat()''。如果该函数尝试取消引用该指针,则该程序的行为将变为未定义。




实际上,该程序的行为在评估

准备调用stat时的第二个参数,无论

stat是如何做的,都会变得不确定。

你需要一个' 'struct stat''指针指向
指向的对象,但没有提供。

...

返回0;

}

与他们有什么不同?
为什么我要修改这样的第二个程序:
{
struct stat temp, * buf =& temp;

stat(argv [1],buf);

...

}



因为你需要一个指针指向的对象。
指针定义不会自动创建指向的对象。这是你的工作。

-Mike




<<删除电子邮件的del>>



dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;

stat(argv[1], buf);

...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}
many thanks

解决方案

"J Wang" <cs****@bath.ac.uk> wrote in message
news:Pi**************************************@amos .bath.ac.uk...


dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;
This is a pointer to a type ''struct stat'' object.
Since you did not initialize it or assign it a value,
this pointer''s value is indeterminate (it doesn''t
point anywhere.)

stat(argv[1], buf);
This statement passes the unknown (i.e. random) pointer
value to the function ''stat()''. If that function attempts
to dereference that pointer, the program''s behavior becomes
undefined.

IOW you need a ''struct stat'' object for the pointer to
point to, but did not provide one.
...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}



Because you need an object for the pointer to point to.
A pointer definition does not automatically create an
object to point to. That''s your job.

-Mike


J Wang wrote:

dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;

stat(argv[1], buf);

...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}
many thanks



The first example is fine because buf is a structure object. The
second doesn''t work because there is no structure object. The third
works because temp is an object and buf points to it.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


On Sun, 18 Jul 2004 14:24:03 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote:

"J Wang" <cs****@bath.ac.uk> wrote in message
news:Pi**************************************@amo s.bath.ac.uk...


dear,

I debug the program recently as follows.

#include <sys/stat.h>

int main(int argc, char *argv[])
{
struct stat buf;

stat(argv[1], &buf);

...
return 0;
}

int main(int argc, char *argv[])
{
struct stat *buf;
This is a pointer to a type ''struct stat'' object.
Since you did not initialize it or assign it a value,
this pointer''s value is indeterminate (it doesn''t
point anywhere.)

stat(argv[1], buf);



This statement passes the unknown (i.e. random) pointer
value to the function ''stat()''. If that function attempts
to dereference that pointer, the program''s behavior becomes
undefined.



Actually, the program''s behavior becomes undefined when it evaluates
the second argument in preparation for calling stat regardless of what
stat does with it.

IOW you need a ''struct stat'' object for the pointer to
point to, but did not provide one.

...

return 0;

}

what is the difference with them?
why I have to modify 2nd program like this:
{
struct stat temp, *buf = &temp;

stat(argv[1], buf);

...

}



Because you need an object for the pointer to point to.
A pointer definition does not automatically create an
object to point to. That''s your job.

-Mike



<<Remove the del for email>>


这篇关于指针和结构变量之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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