By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

what difference between pointer and struct variable

P: n/a

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

Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"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
Nov 14 '05 #2

P: n/a
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 ---
Nov 14 '05 #3

P: n/a
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>>
Nov 14 '05 #4

P: n/a
>>
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.


This program invokes undefined behavior before the function stat()
is called, because an uninitialized pointer is used. The result
is undefined behavior whether or not stat() dereferences or even
uses its second argument.

Gordon L. Burditt
Nov 14 '05 #5

P: n/a
Like you said, the first example uses a variable, and the second one a
pointer, thus their behave is different. In the first example you
created a variable of the type 'struct stat' you passed it's address
using the '&' operator to the function stat. When you declared the
variable buf, you "reserved" a space in memory to store some data. So
when you pass the address of this space the function acts in it, doing
the job it's intended to.

In the second example, you declared a variable of the type 'pointer to
struct stat'. When you declare it, it's value can't be determined, so
it's what we call a 'junk pointer' and it's pointing to some place we
don't know. What you're doing is saying to the function stat to read
in this undetermined place, but since you don't know where it's
pointing, it may (and probably will) be in a place where you can't
access. So, in order to make your second example work, you should make
your pointer point to some place where you know you have access. To do
it you could use dynamic memory allocation...

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

buf = malloc(sizeof(struct stat));

stat(argv[1], buf);

...

free(buf);

return 0;

}

or you could do like in your third example.

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; 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

Nov 14 '05 #6

P: n/a
Gustavo Cipriano Mota Sousa <gu*********@inf.ufg.br> spoke thus:
struct stat *buf;
buf = malloc(sizeof(struct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.

--
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.
Nov 14 '05 #7

P: n/a

On Mon, 19 Jul 2004, Christopher Benson-Manica wrote:

Gustavo Cipriano Mota Sousa <gu*********@inf.ufg.br> spoke thus:
struct stat *buf;
buf = malloc(sizeof(struct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.


Except that the redundant parentheses might as well be lost, too.
Simplicity is clarity. (Note also how my religion w.r.t. whitespace
is basically the exact opposite of Chris's;)

buf = malloc(sizeof *buf);

-Arthur

Nov 14 '05 #8

P: n/a
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> spoke thus:
Except that the redundant parentheses might as well be lost, too.
Simplicity is clarity. (Note also how my religion w.r.t. whitespace
is basically the exact opposite of Chris's;) buf = malloc(sizeof *buf);


It's my employer's religion, actually, but yes :)

--
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.
Nov 14 '05 #9

P: n/a
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:cd**********@chessie.cirr.com...
Gustavo Cipriano Mota Sousa <gu*********@inf.ufg.br> spoke thus:
struct stat *buf;
buf = malloc(sizeof(struct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.


Except if buf is altered to an incorrect declaration. Not casting malloc does not save the
programmer in this regard.

--
Peter
Nov 14 '05 #10

P: n/a
On Tue, 20 Jul 2004 11:18:21 +1000, in comp.lang.c , "Peter Nilsson"
<ai***@acay.com.au> wrote:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:cd**********@chessie.cirr.com...
Gustavo Cipriano Mota Sousa <gu*********@inf.ufg.br> spoke thus:
> struct stat *buf;
> buf = malloc(sizeof(struct stat));


1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.


Except if buf is altered to an incorrect declaration. Not casting malloc does not save the
programmer in this regard.


What could buf be altered to that would not either cause a compiler
warning, or be a perfectly valid object? Apart from a function pointer.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #11

P: n/a
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:s8********************************@4ax.com...
On Tue, 20 Jul 2004 11:18:21 +1000, in comp.lang.c , "Peter Nilsson"
<ai***@acay.com.au> wrote:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in messagenews:cd**********@chessie.cirr.com...
Gustavo Cipriano Mota Sousa <gu*********@inf.ufg.br> spoke thus:

> struct stat *buf;
> buf = malloc(sizeof(struct stat));

1) OP should check whether malloc() succeeded, of course.
2) The preferred idiom is

buf=malloc( sizeof(*buf) );

because it will not break if the declaration of buf is altered.


Except if buf is altered to an incorrect declaration. Not casting malloc does not save theprogrammer in this regard.


What could buf be altered to that would not either cause a compiler
warning, or be a perfectly valid object? Apart from a function pointer.


Any type that is logically inconsistent with the required type, e.g.
allocating a pointer to ints when a pointer to longs is assumed elsewhere in
the code.

--
Peter
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.