structs and pointers

Discussion in 'C Programming' started by Bill Cunningham, Apr 28, 2009.

  1. I have this code written like this:

    #include <stdio.h>
    #include <sys/stat.h>

    int main()
    {
    int i;
    struct stat st;
    if ((i = stat("/bin/e", &st)) == -1) {
    fputs("stat error", stderr);
    return -1;
    }
    printf("%i\n", st.st_blocks);
    printf("%i\n", st.st_blksize);
    return 0;
    }

    Now if I wanted instead of

    struct stat st;
    struct stat *st;

    How would that change the second parameter to stat()? Would it be st instead
    of &st, or *st ?

    Bill
    Bill Cunningham, Apr 28, 2009
    #1
    1. Advertising

  2. Bill Cunningham

    Lew Pitcher Guest

    On April 28, 2009 13:29, in comp.lang.c, Bill Cunningham
    () wrote:

    > I have this code written like this:
    >
    > #include <stdio.h>
    > #include <sys/stat.h>
    >
    > int main()
    > {
    > int i;
    > struct stat st;
    > if ((i = stat("/bin/e", &st)) == -1) {
    > fputs("stat error", stderr);
    > return -1;
    > }
    > printf("%i\n", st.st_blocks);
    > printf("%i\n", st.st_blksize);
    > return 0;
    > }
    >
    > Now if I wanted instead of
    >
    > struct stat st;
    > struct stat *st;
    >
    > How would that change the second parameter to stat()? Would it be st
    > instead of &st, or *st ?


    Hi, Bill

    Take a look at the following code, based on your example above...

    #include <stdio.h>
    #include <sys/stat.h>

    int main()
    {
    int i;

    struct stat statstruct;
    struct stat *st;

    /* Initialize st
    ** st is a "pointer to struct stat", so
    ** it takes the address of a struct stat
    ** to initialize st
    */
    st = &statstruct;

    /* stat() needs a "pointer to struct stat"
    ** and that's exactly what st is
    */
    if ((i = stat("/bin/e", st)) == -1) {
    fputs("stat error", stderr);
    return -1;
    }

    /* st is a "pointer to struct stat"
    ** so, to access the struct stat members
    ** through st, we have to dereference the
    ** pointer. Two methods are shown
    */
    printf("%i\n", (*st).st_blocks); /* method 1 - *(st)->member */
    printf("%i\n", st->st_blksize); /* method 2 - st->member */

    return 0;
    }

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Apr 28, 2009
    #2
    1. Advertising

  3. "Richard" <> wrote in message
    news:gt7ght$7ru$-september.org...
    > 2/10. You're slipping Bill.
    >
    > The return of 0 and -1 and use of stat was a nice touch though. But
    > surely even you don't expect us to believe you are still totally
    > incompetent and don't know what a pointer is still?


    I know what a pointer is. Working with them is different. Dereferencing
    is my problem.

    You need some new
    > ideas. Try floats - Falconer will be along to make a fool of himself
    > once more.
    Bill Cunningham, Apr 28, 2009
    #3
  4. "Han from China" <> wrote in message
    news:...

    > It would be st. But you'd have to make sure you've allocated space.
    > Search for the recent thread "problem on fonction mkdir" (misspelling
    > intentional) to see someone using stat() in that manner. Unless
    > you have a good reason not to use the &st form, stick with &st.


    That makes sense. Since I don't fully understand working with pointers.
    The prototype of stat()'s second parameter takes a pointer to type struct.
    So I want to declare a pointer like such. struct stat *sp;

    I am reading prototypes a little better now.

    Bill
    Bill Cunningham, Apr 28, 2009
    #4
  5. "Richard" <> wrote in message
    news:gt7v4i$bi5$-september.org...

    > You don't understand how to dereference a pointer even though you know
    > what a pointer is?
    >
    > You never saw "*p" ?


    Yes that's declaring a pointer. char *p;
    Bill Cunningham, Apr 29, 2009
    #5
  6. "Bill Cunningham" <> writes:
    > "Richard" <> wrote in message
    > news:gt7v4i$bi5$-september.org...
    >
    >> You don't understand how to dereference a pointer even though you know
    >> what a pointer is?
    >>
    >> You never saw "*p" ?

    >
    > Yes that's declaring a pointer. char *p;


    It's also the syntax for dereferencing a pointer.

    #include <stdio.h>
    int main(void)
    {
    int n = 42;
    int *p; /* declared p as a pointer; p's type is int* */
    p = &n;
    printf("*p = %d\n", *p); /* The "*" operator dereferences p */
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 29, 2009
    #6
  7. "Keith Thompson" <> wrote in message
    news:...

    > It's also the syntax for dereferencing a pointer.
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > int n = 42;
    > int *p; /* declared p as a pointer; p's type is int* */
    > p = &n;
    > printf("*p = %d\n", *p); /* The "*" operator dereferences p */
    > return 0;
    > }

    int *p pointer to int
    p=&n pointer p points to the location in memory allocated for int n where
    the value 42 is in memory.
    The printf line is totally confusing and throughs me off. Why would one want
    to dereference? If you want to print the location in memory p points to I
    guess you would do this.

    printf("%p\n",p);

    If you wanted to print the value at n's locaation in memory which is stored
    there ie 42 Then would you use

    printf("%i\n",*p) ???

    I've heard of dereferencing but have no idea what it is. I am thinking
    pointers are being declared.

    Bill
    Bill Cunningham, Apr 29, 2009
    #7
  8. "Bill Cunningham" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >
    >> It's also the syntax for dereferencing a pointer.
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> int n = 42;
    >> int *p; /* declared p as a pointer; p's type is int* */
    >> p = &n;
    >> printf("*p = %d\n", *p); /* The "*" operator dereferences p */
    >> return 0;
    >> }

    > int *p pointer to int
    > p=&n pointer p points to the location in memory allocated for int n where
    > the value 42 is in memory.
    > The printf line is totally confusing and throughs me off. Why would one want
    > to dereference? If you want to print the location in memory p points to I
    > guess you would do this.
    >
    > printf("%p\n",p);
    >
    > If you wanted to print the value at n's locaation in memory which is stored
    > there ie 42 Then would you use
    >
    > printf("%i\n",*p) ???


    Note that that's almost the same thing as the printf statement that I
    wrote, that you found so confusing. "%i" and "%d" mean the same thing
    in format strings.

    Dereferencing a pointer means getting what it points to. p is a
    pointer object; its value is an address (it happens to be the address
    of n). *p is an int object, the same object as n. The value of *p is
    42.

    > I've heard of dereferencing but have no idea what it is. I am thinking
    > pointers are being declared.


    Declaring a pointer and dereferencing a pointer are very different
    things, though they use similar syntax (for reasons I won't get into).

    A pointer object contains an address. Dereferencing the pointer
    object gives you the thing that the pointer points to.

    (I see that "Han from China" and Richard nolastname have posted in
    this thread. Be aware that I won't see anything they write.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 29, 2009
    #8
  9. Bill Cunningham

    R J C Guest

    Keith Thompson scribbled:
    > (I see that "Han from China" and Richard nolastname have posted in
    > this thread. Be aware that I won't see anything they write.)


    Keith, I know Han from China and Richard stir the pot on this newsgroup
    and probably deserve not to be read, but you /do/ come across as being
    somewhat obsessed. Neither Han from China nor Richard said anything
    about you in this thread, and therefore one does have to wonder what
    your problem is and why you seem to enjoy inciting the same drama
    they seem to enjoy inciting. I've been lurking this newsgroup, and even
    tho I've criticized HfC's hyper-ISO mode as being deliberately
    unhelpful, he /does/ help a lot of people. I've seen you help a lot of
    people too. If only as a matter of self-marketing, you should give the
    jabs a rest unless there is an obvious need for them {and I think we
    can both agree that there will be an obvious need sooner or later}, for
    they do your image no favors and quite a bit of damage.

    My $0.2

    Rob
    R J C, Apr 29, 2009
    #9
  10. R J C <> writes:
    > Keith Thompson scribbled:
    >> (I see that "Han from China" and Richard nolastname have posted in
    >> this thread. Be aware that I won't see anything they write.)

    >
    > Keith, I know Han from China and Richard stir the pot on this newsgroup
    > and probably deserve not to be read, but you /do/ come across as being
    > somewhat obsessed. Neither Han from China nor Richard said anything
    > about you in this thread, and therefore one does have to wonder what
    > your problem is and why you seem to enjoy inciting the same drama
    > they seem to enjoy inciting. I've been lurking this newsgroup, and even
    > tho I've criticized HfC's hyper-ISO mode as being deliberately
    > unhelpful, he /does/ help a lot of people. I've seen you help a lot of
    > people too. If only as a matter of self-marketing, you should give the
    > jabs a rest unless there is an obvious need for them {and I think we
    > can both agree that there will be an obvious need sooner or later}, for
    > they do your image no favors and quite a bit of damage.


    No jab was intended, though I can see how it might have appeared that
    way. I merely wanted Bill Cunningham to be aware that, whatever HfC
    and Richard write in this thread (and apart from your brief account I
    honestly have no idea what that might be), I would not be reading it.
    For example, if they comment on something I wrote, no conclusions
    should be drawn from my failure to respond. In this particular
    thread, I thought it was particularly important to do what I can to
    avoid any more confusion than already exists.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 29, 2009
    #10
  11. In article <>,
    Keith Thompson <> wrote:
    ....
    >(I see that "Han from China" and Richard nolastname have posted in
    >this thread. Be aware that I won't see anything they write.)


    I can't see you! Really, really, I can't!

    But I know you are there (because of my Extra Special Powers!)
    Kenny McCormack, Apr 29, 2009
    #11
  12. On Tue, 28 Apr 2009 15:38:32 -0400, "Bill Cunningham"
    <> wrote:

    >
    >"Han from China" <> wrote in message
    >news:...
    >
    >> It would be st. But you'd have to make sure you've allocated space.
    >> Search for the recent thread "problem on fonction mkdir" (misspelling
    >> intentional) to see someone using stat() in that manner. Unless
    >> you have a good reason not to use the &st form, stick with &st.

    >
    > That makes sense. Since I don't fully understand working with pointers.
    >The prototype of stat()'s second parameter takes a pointer to type struct.
    >So I want to declare a pointer like such. struct stat *sp;


    Not necessarily. Your original code passed a pointer to struct by
    taking the address of an existing struct. Not knowing anything about
    stat(), all I can say is that most functions that take a pointer to
    struct really expect this. You only need to declare a pointer to
    struct object when your code needs one. In this case it doesn't.

    If you actually passed a pointer object to the function, you would
    have to insure the pointer object contained a valid value. The only
    two valid values would be the address of such a struct (or the address
    of a block of allocated memory at least large enough to hold such a
    struct) or NULL. And many functions are not prepared to handle NULL
    values.

    >I am reading prototypes a little better now.


    Prototypes provide only the MINIMUM amount of information you need to
    code a function call. They do not tell you anything about how the
    function uses the arguments. If you don't know that, your chances of
    using the function are somewhat remote.

    In this case, I expect the function will attempt to store values in
    the struct being pointed to. There are other cases where the function
    requires a pointer to struct simply because it is usually more
    efficient to pass a pointer than to pass the entire struct.

    --
    Remove del for email
    Barry Schwarz, Apr 29, 2009
    #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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    621
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    548
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    630
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,375
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    464
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page