what difference between pointer and struct variable

Discussion in 'C Programming' started by J Wang, Jul 18, 2004.

  1. J Wang

    J Wang Guest

    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, Jul 18, 2004
    #1
    1. Advertising

  2. J Wang

    Mike Wahler Guest

    "J Wang" <> wrote in message
    news:p...
    >
    > 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
     
    Mike Wahler, Jul 18, 2004
    #2
    1. Advertising

  3. J Wang

    Joe Wright Guest

    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:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 18, 2004
    #3
  4. On Sun, 18 Jul 2004 14:24:03 GMT, "Mike Wahler"
    <> wrote:

    >"J Wang" <> wrote in message
    >news:p...
    >>
    >> 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>>
     
    Barry Schwarz, Jul 18, 2004
    #4
  5. >>
    >> 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
     
    Gordon Burditt, Jul 18, 2004
    #5
  6. 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 <> wrote in message news:<>...

    > 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
     
    Gustavo Cipriano Mota Sousa, Jul 18, 2004
    #6
  7. Gustavo Cipriano Mota Sousa <> 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.
     
    Christopher Benson-Manica, Jul 19, 2004
    #7
  8. On Mon, 19 Jul 2004, Christopher Benson-Manica wrote:
    >
    > Gustavo Cipriano Mota Sousa <> 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
     
    Arthur J. O'Dwyer, Jul 19, 2004
    #8
  9. Arthur J. O'Dwyer <> 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.
     
    Christopher Benson-Manica, Jul 19, 2004
    #9
  10. "Christopher Benson-Manica" <> wrote in message
    news:cdgiqg$bc9$...
    > Gustavo Cipriano Mota Sousa <> 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
     
    Peter Nilsson, Jul 20, 2004
    #10
  11. On Tue, 20 Jul 2004 11:18:21 +1000, in comp.lang.c , "Peter Nilsson"
    <> wrote:

    >"Christopher Benson-Manica" <> wrote in message
    >news:cdgiqg$bc9$...
    >> Gustavo Cipriano Mota Sousa <> 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 =---
     
    Mark McIntyre, Jul 20, 2004
    #11
  12. "Mark McIntyre" <> wrote in message
    news:...
    > On Tue, 20 Jul 2004 11:18:21 +1000, in comp.lang.c , "Peter Nilsson"
    > <> wrote:
    >
    > >"Christopher Benson-Manica" <> wrote in

    message
    > >news:cdgiqg$bc9$...
    > >> Gustavo Cipriano Mota Sousa <> 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.


    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
     
    Peter Nilsson, Jul 24, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,390
  2. beetle
    Replies:
    2
    Views:
    926
    beetle
    Jan 25, 2005
  3. Zero
    Replies:
    16
    Views:
    667
    Barry Schwarz
    Nov 19, 2005
  4. Pep
    Replies:
    4
    Views:
    837
    James Kanze
    Sep 10, 2009
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    485
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page