what difference between pointer and struct variable

J

J Wang

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
 
M

Mike Wahler

J Wang said:
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

Joe Wright

J said:
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.
 
B

Barry Schwarz

J Wang said:
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>>
 
G

Gordon Burditt

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
 
G

Gustavo Cipriano Mota Sousa

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 said:
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
 
C

Christopher Benson-Manica

Gustavo Cipriano Mota Sousa said:
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.
 
A

Arthur J. O'Dwyer

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
 
C

Christopher Benson-Manica

Arthur J. O'Dwyer said:
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 :)
 
P

Peter Nilsson

Christopher Benson-Manica said:
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.
 
M

Mark McIntyre

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.
 
P

Peter Nilsson

Mark McIntyre said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top