473,372 Members | 1,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

what difference between pointer and struct variable


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
11 3371
"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
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
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
>>
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
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
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

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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
4
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
5
by: alcool | last post by:
hi, I have 2 date/time values i.e. the system date/time and (h:m dd:mm:yyyy). I would like know to find a routine that calculate this difference. Maybe using the struct time_t and difftime. ...
11
by: Neo | last post by:
If i have a : typedef struct str32{ uint32_t word1; uint32_t word2; } word_array; word_array *my_array; what would be the data type of this: myarray->word1
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.