structure member access issue

Discussion in 'C Programming' started by Waxhead, Sep 10, 2005.

  1. Waxhead

    Waxhead Guest

    Note: I'm not experienced with newsgroups so my apologies if I've done
    any errors.

    In the function dosomething() I would like to skip the workingstuffptr
    "workaround". is there any way I can use stuffptr directly without
    using a temp?

    Thanks in advance.



    #include <stdio.h>
    #include <stdlib.h>

    typedef struct _stuff
    {
    unsigned int value;
    } STUFF, *LPSTUFF;

    void dosomething(LPSTUFF *stuffptr)
    {
    STUFF *workingstuffptr;

    workingstuffptr = *stuffptr;
    workingstuffptr->value = 5;

    stuffptr->id = 5; /* Can't be done */

    }

    int main(void)
    {
    LPSTUFF somestuff;

    somestuff = malloc(sizeof(STUFF));
    if(!somestuff)
    return 5;
    somestuff->value = 10;

    dosomething(&somestuff);

    printf("Value = %d\n",somestuff->value);
    return 0;
    }
     
    Waxhead, Sep 10, 2005
    #1
    1. Advertising

  2. "Waxhead" <> wrote:
    >Note: I'm not experienced with newsgroups so my apologies if I've done
    >any errors.
    >
    >In the function dosomething() I would like to skip the workingstuffptr
    >"workaround". is there any way I can use stuffptr directly without
    >using a temp?
    >
    >Thanks in advance.
    >
    >
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >typedef struct _stuff


    Identifiers beginning with an underscore are reserved for the
    implementation.

    >{
    > unsigned int value;
    >} STUFF, *LPSTUFF;


    Note that hiding structural types with typedef is often considered
    very bad style, hiding pointer types with a typedef as well.

    >void dosomething(LPSTUFF *stuffptr)


    Why do you pass a pointer-to-pointer? Actually this looks like a
    good example how a programmer got confused by his own typedef'ing
    practice.

    >{
    > STUFF *workingstuffptr;


    Why not be use LPSTUFF here as well? At least be consistent with your
    typename aliases.

    >
    > workingstuffptr = *stuffptr;
    > workingstuffptr->value = 5;
    >
    > stuffptr->id = 5; /* Can't be done */


    An for two obvious reasons:
    1. stuffptr is a pointer-to-a-pointer-to-struct _stuff
    2. your struct _stuff has no member named id declared.

    >
    >}
    >
    >int main(void)
    >{
    > LPSTUFF somestuff;
    >
    > somestuff = malloc(sizeof(STUFF));


    The c.l.c approved idiom for this is:

    somestuff = malloc(sizeof *somestuff);

    At least you didn't commit the crime to cast the result of malloc. :)
    > if(!somestuff)
    > return 5;


    5 is not a portable return value for main. Since you already included
    stdlib.h EXIT_FAILURE and EXIT_SUCCESS are at your command.

    > somestuff->value = 10;
    >
    > dosomething(&somestuff);


    Blindly adding levels of indirection, probably because the compiler
    barfed, is not the solution

    >
    > printf("Value = %d\n",somestuff->value);


    %d is not the right format specifier to print an unsigned int. Use %u
    instead.

    > return 0;
    >}


    For a beginner your code is not too bad, actually (I've definitely
    seen worse), but certainly no cigar. :)

    Conclusion: throw out those hideous typedefs, and then rewrite the
    program logic. Below you find a corrected and cleaned up version of
    your code, don't look if you want to learn by doing it yourself.

    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..

    #include <stdio.h>
    #include <stdlib.h>

    struct stuff
    {
    unsigned int value;
    };

    void dosomething(struct stuff *stuffptr)
    {
    stuffptr->value = 5;
    }

    int main(void)
    {
    struct stuff *somestuff;

    somestuff = malloc(sizeof *somestuff);
    if(somestuff == NULL)
    return EXIT_FAILURE;

    somestuff->value = 10;
    dosomething(somestuff);
    printf("Value = %u\n",somestuff->value);
    return EXIT_SUCCESS;
    }


    Best regards
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc.
     
    Irrwahn Grausewitz, Sep 10, 2005
    #2
    1. Advertising

  3. Waxhead

    Eric Sosman Guest

    Waxhead wrote:
    > Note: I'm not experienced with newsgroups so my apologies if I've done
    > any errors.
    >
    > In the function dosomething() I would like to skip the workingstuffptr
    > "workaround". is there any way I can use stuffptr directly without
    > using a temp?
    >
    > Thanks in advance.
    >
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > typedef struct _stuff
    > {
    > unsigned int value;
    > } STUFF, *LPSTUFF;
    >
    > void dosomething(LPSTUFF *stuffptr)
    > {
    > STUFF *workingstuffptr;
    >
    > workingstuffptr = *stuffptr;
    > workingstuffptr->value = 5;
    >
    > stuffptr->id = 5; /* Can't be done */


    (*stuffptr)->id = 5; /* Can be */

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 10, 2005
    #3
  4. Eric Sosman <> wrote:
    >Waxhead wrote:

    <snip>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> typedef struct _stuff
    >> {
    >> unsigned int value;
    >> } STUFF, *LPSTUFF;
    >>
    >> void dosomething(LPSTUFF *stuffptr)
    >> {
    >> STUFF *workingstuffptr;
    >>
    >> workingstuffptr = *stuffptr;
    >> workingstuffptr->value = 5;
    >>
    >> stuffptr->id = 5; /* Can't be done */

    >
    > (*stuffptr)->id = 5; /* Can be */


    Nice try, but: no. :)
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc.
     
    Irrwahn Grausewitz, Sep 10, 2005
    #4
  5. Waxhead

    Eric Sosman Guest

    Irrwahn Grausewitz wrote:
    > Eric Sosman <> wrote:
    >
    >>Waxhead wrote:

    >
    > <snip>
    >
    >>>#include <stdio.h>
    >>>#include <stdlib.h>
    >>>
    >>>typedef struct _stuff
    >>>{
    >>> unsigned int value;
    >>>} STUFF, *LPSTUFF;
    >>>
    >>>void dosomething(LPSTUFF *stuffptr)
    >>>{
    >>> STUFF *workingstuffptr;
    >>>
    >>> workingstuffptr = *stuffptr;
    >>> workingstuffptr->value = 5;
    >>>
    >>> stuffptr->id = 5; /* Can't be done */

    >>
    >> (*stuffptr)->id = 5; /* Can be */

    >
    >
    > Nice try, but: no. :)


    Why not, pray? Oh, I see: the O.P. switched gears from
    "value" to "id" somewhere in mid-stream, but assuming that
    was just a typo, the (*stuffptr)->element syntax is correct.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 10, 2005
    #5
  6. Eric Sosman <> wrote:
    >Irrwahn Grausewitz wrote:
    >> Eric Sosman <> wrote:
    >>>Waxhead wrote:

    >>

    <snip>
    >>>>typedef struct _stuff
    >>>>{
    >>>> unsigned int value;
    >>>>} STUFF, *LPSTUFF;
    >>>>
    >>>>void dosomething(LPSTUFF *stuffptr)

    <snip>
    >>>> stuffptr->id = 5; /* Can't be done */
    >>>
    >>> (*stuffptr)->id = 5; /* Can be */

    >>
    >>
    >> Nice try, but: no. :)

    >
    > Why not, pray? Oh, I see: the O.P. switched gears from
    >"value" to "id" somewhere in mid-stream, but assuming that
    >was just a typo, the (*stuffptr)->element syntax is correct.


    Correct, and to be fair, I should've said so. :]
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
     
    Irrwahn Grausewitz, Sep 10, 2005
    #6
  7. Waxhead

    Waxhead Guest

    Well thanks for pointing out my errors (as I expected). and yes I
    forgot to change ->id to ->value since this was just a quick copy of
    some code I have running ;)

    Thanks for the help ;)
     
    Waxhead, Sep 11, 2005
    #7
  8. Waxhead wrote on 10/09/05 :
    > typedef struct _stuff
    > {
    > unsigned int value;
    > } STUFF, *LPSTUFF;
    >
    > void dosomething(LPSTUFF *stuffptr)
    > {
    > STUFF *workingstuffptr;
    >
    > workingstuffptr = *stuffptr;


    > stuffptr->id = 5; /* Can't be done */


    'id' is not part of the structure. Let's say you want 'value'.

    'stuffptr' being a pointer to a pointer, you need an extra indirection:

    (*stuffptr)->id = 5;


    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "It's specified. But anyone who writes code like that should be
    transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
     
    Emmanuel Delahaye, Sep 11, 2005
    #8
  9. Irrwahn Grausewitz wrote on 10/09/05 :
    >> typedef struct _stuff

    >
    > Identifiers beginning with an underscore are reserved for the
    > implementation.


    Well, strictly speaking, and AFAIK, id's beginning by a _ and followed
    by a lowercase are not reserved. But they are ugly enough to me to
    avoid them.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "It's specified. But anyone who writes code like that should be
    transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
     
    Emmanuel Delahaye, Sep 11, 2005
    #9
  10. "Emmanuel Delahaye" <> wrote:
    >Irrwahn Grausewitz wrote on 10/09/05 :
    >>> typedef struct _stuff

    >>
    >> Identifiers beginning with an underscore are reserved for the
    >> implementation.

    >
    >Well, strictly speaking, and AFAIK, id's beginning by a _ and followed
    >by a lowercase are not reserved.


    Oops, you're right.

    >But they are ugly enough to me to
    >avoid them.


    Same to me. :)

    Best Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
     
    Irrwahn Grausewitz, Sep 11, 2005
    #10
  11. "Waxhead" <> wrote:
    >Well thanks for pointing out my errors (as I expected). and yes I
    >forgot to change ->id to ->value since this was just a quick copy of
    >some code I have running ;)
    >
    >Thanks for the help ;)


    No problem, but please preserve some context when posting replies,
    it's otherwise difficult to tell to whom or what you are responding.

    -- If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers. --

    Best Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc frequent answers: http://benpfaff.org/writings/clc
     
    Irrwahn Grausewitz, Sep 11, 2005
    #11
  12. "Waxhead" <> writes:
    > Well thanks for pointing out my errors (as I expected). and yes I
    > forgot to change ->id to ->value since this was just a quick copy of
    > some code I have running ;)


    This is a good illustration of why, when posting code, you should
    *always* cut-and-paste the actual code you fed to the compiler. If
    you try to transcribe it manually, you'll inevitably make mistakes (I
    do this myself), and it may be impossible for us to guess what your
    actual problem is.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 11, 2005
    #12
  13. Waxhead

    Joe Wright Guest

    Keith Thompson wrote:
    > "Waxhead" <> writes:
    >
    >>Well thanks for pointing out my errors (as I expected). and yes I
    >>forgot to change ->id to ->value since this was just a quick copy of
    >>some code I have running ;)

    >
    >
    > This is a good illustration of why, when posting code, you should
    > *always* cut-and-paste the actual code you fed to the compiler. If
    > you try to transcribe it manually, you'll inevitably make mistakes (I
    > do this myself), and it may be impossible for us to guess what your
    > actual problem is.
    >


    Nit-pick, but it's copy-and-paste you want. Cutting the code out of the
    source file would not be "A Good Thing".

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Sep 13, 2005
    #13
    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. Ph. Barthelemy
    Replies:
    3
    Views:
    1,179
    Gordon Beaton
    Jan 5, 2005
  2. ding feng
    Replies:
    8
    Views:
    854
    Dhruv
    Jul 2, 2003
  3. Siemel Naran
    Replies:
    4
    Views:
    809
    Micah Cowan
    Jan 12, 2005
  4. dobest03
    Replies:
    2
    Views:
    497
    Mark P
    May 4, 2006
  5. Mark

    macro to access structure's member

    Mark, Oct 17, 2011, in forum: C Programming
    Replies:
    8
    Views:
    1,103
Loading...

Share This Page