typedef question

Discussion in 'C Programming' started by Logan Lee, Dec 28, 2007.

  1. Logan Lee

    Logan Lee Guest

    ORIGINAL CODE:
    typedef struct StackStruct *Stack;
    typedef void *StackElt;

    StackElt top(Stack thisStack);

    ANOTHER CODE:
    typedef struct StackStruct Stack;
    typedef void *StackElt;

    StackElt top(Stack thisStack);
    -

    Is ANOTHER CODE correct? If it is correct, then in what cases would you use
    ANOTHER CODE and in what cases would you use the other one?
     
    Logan Lee, Dec 28, 2007
    #1
    1. Advertising

  2. Logan Lee <> writes:

    > ORIGINAL CODE:
    > typedef struct StackStruct *Stack;
    > typedef void *StackElt;
    >
    > StackElt top(Stack thisStack);
    >
    > ANOTHER CODE:
    > typedef struct StackStruct Stack;
    > typedef void *StackElt;
    >
    > StackElt top(Stack thisStack);
    > -
    >
    > Is ANOTHER CODE correct?


    Well, it would work.

    > If it is correct, then in what cases would you use
    > ANOTHER CODE and in what cases would you use the other one?


    Passing a structure to a function is perfectly correct in C but is not
    often done in cases like this (implementing data structures). This is
    because it can be expensive (arguments are passed by value so the
    whole structure has to be copied) and because the function can't
    modify the structure in a way that the calling function can see (the
    called function gets a copy, remember).

    In the case of returning the top element of a stack, this second point
    does not matter -- the function has no need to change anything -- but
    consistency is important and other stack function do need to modify
    the data so they would need to be passed a pointer to a Stack object.

    Many programmers will opt for a mix of your two examples -- passing a
    pointer to every function that operates on a Stack, but using the
    second set of typedefs so the * is written explicitly in every
    function prototype.

    --
    Ben.
     
    Ben Bacarisse, Dec 28, 2007
    #2
    1. Advertising

  3. Logan Lee

    Richard Bos Guest

    Logan Lee <> wrote:

    > ORIGINAL CODE:
    > typedef struct StackStruct *Stack;
    > ANOTHER CODE:
    > typedef struct StackStruct Stack;


    > Is ANOTHER CODE correct? If it is correct, then in what cases would you use
    > ANOTHER CODE and in what cases would you use the other one?


    How about doing your own homework?

    Richard
     
    Richard Bos, Dec 28, 2007
    #3
  4. Logan Lee

    Eric Sosman Guest

    Logan Lee wrote:
    > ORIGINAL CODE:
    > typedef struct StackStruct *Stack;
    > typedef void *StackElt;
    >
    > StackElt top(Stack thisStack);
    >
    > ANOTHER CODE:
    > typedef struct StackStruct Stack;
    > typedef void *StackElt;
    >
    > StackElt top(Stack thisStack);
    > -
    >
    > Is ANOTHER CODE correct?


    No, as your compiler would have told you had you asked it.

    > If it is correct, then in what cases would you use
    > ANOTHER CODE and in what cases would you use the other one?


    I would use ANOTHER CODE if writing a test suite to see
    whether a compiler generated the diagnostics it should.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 28, 2007
    #4
  5. Eric Sosman <> writes:
    > Logan Lee wrote:
    >> ORIGINAL CODE:
    >> typedef struct StackStruct *Stack;
    >> typedef void *StackElt;
    >>
    >> StackElt top(Stack thisStack);
    >>
    >> ANOTHER CODE:
    >> typedef struct StackStruct Stack;
    >> typedef void *StackElt;
    >>
    >> StackElt top(Stack thisStack);
    >> -
    >>
    >> Is ANOTHER CODE correct?

    >
    > No, as your compiler would have told you had you asked it.
    >
    >> If it is correct, then in what cases would you use
    >> ANOTHER CODE and in what cases would you use the other one?

    >
    > I would use ANOTHER CODE if writing a test suite to see
    > whether a compiler generated the diagnostics it should.


    And what diagnostics should it generate? Mine didn't generate any,
    and I don't see anything in those three lines of code that requires a
    diagnostic. It's not particularly good style, but it's legal.

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 28, 2007
    #5
  6. Logan Lee

    Eric Sosman Guest

    Keith Thompson wrote:
    > Eric Sosman <> writes:
    >> Logan Lee wrote:
    >>> ORIGINAL CODE:
    >>> typedef struct StackStruct *Stack;
    >>> typedef void *StackElt;
    >>>
    >>> StackElt top(Stack thisStack);
    >>>
    >>> ANOTHER CODE:
    >>> typedef struct StackStruct Stack;
    >>> typedef void *StackElt;
    >>>
    >>> StackElt top(Stack thisStack);
    >>> -
    >>>
    >>> Is ANOTHER CODE correct?

    >> No, as your compiler would have told you had you asked it.
    >>
    >>> If it is correct, then in what cases would you use
    >>> ANOTHER CODE and in what cases would you use the other one?

    >> I would use ANOTHER CODE if writing a test suite to see
    >> whether a compiler generated the diagnostics it should.

    >
    > And what diagnostics should it generate? Mine didn't generate any,
    > and I don't see anything in those three lines of code that requires a
    > diagnostic. It's not particularly good style, but it's legal.


    The compiler I use says

    foo.c:4: warning: parameter has incomplete type

    .... and if your compiler doesn't say something similar, I suspect
    you've added something to the code the O.P. posted.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 28, 2007
    #6
  7. Eric Sosman <> writes:
    > Keith Thompson wrote:
    >> Eric Sosman <> writes:
    >>> Logan Lee wrote:
    >>>> ORIGINAL CODE:
    >>>> typedef struct StackStruct *Stack;
    >>>> typedef void *StackElt;
    >>>>
    >>>> StackElt top(Stack thisStack);
    >>>>
    >>>> ANOTHER CODE:
    >>>> typedef struct StackStruct Stack;
    >>>> typedef void *StackElt;
    >>>>
    >>>> StackElt top(Stack thisStack);
    >>>> -
    >>>>
    >>>> Is ANOTHER CODE correct?
    >>> No, as your compiler would have told you had you asked it.
    >>>
    >>>> If it is correct, then in what cases would you use
    >>>> ANOTHER CODE and in what cases would you use the other one?
    >>> I would use ANOTHER CODE if writing a test suite to see
    >>> whether a compiler generated the diagnostics it should.

    >>
    >> And what diagnostics should it generate? Mine didn't generate any,
    >> and I don't see anything in those three lines of code that requires a
    >> diagnostic. It's not particularly good style, but it's legal.

    >
    > The compiler I use says
    >
    > foo.c:4: warning: parameter has incomplete type
    >
    > ... and if your compiler doesn't say something similar, I suspect
    > you've added something to the code the O.P. posted.


    Interesting. That look like a gcc warning, but I get:

    % cat c.c
    typedef struct StackStruct Stack;
    typedef void *StackElt;
    StackElt top(Stack thisStack);
    % gcc --version | head -1
    gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
    % gcc -ansi -pedantic -Wall -W -c c.c
    %

    If I tweak the code, I get a warning if I try to call the function,
    but as long as I define ``struct StackStruct'' before the call,
    there's no complaint.

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 28, 2007
    #7
  8. On Fri, 28 Dec 2007 11:30:26 -0800, Keith Thompson wrote:
    > Eric Sosman <> writes:
    >>>> Logan Lee wrote:
    >>>>> typedef struct StackStruct Stack;
    >>>>> typedef void *StackElt;
    >>>>>
    >>>>> StackElt top(Stack thisStack);

    >>
    >> The compiler I use says
    >>
    >> foo.c:4: warning: parameter has incomplete type
    >>
    >> ... and if your compiler doesn't say something similar, I suspect
    >> you've added something to the code the O.P. posted.

    >
    > Interesting. That look like a gcc warning,


    It is. It's an unnecessary warning that older versions of gcc used to
    give, and newer versions don't, since function parameters in function
    declarations that are not definitions are allowed to have incomplete
    types in standard C. I can't find anything prohibiting even nonsense
    declarations that cannot be completed:

    static void f(void arg1, void arg2);
    int main(void) { /* nothing */ }
     
    Harald van Dijk, Dec 28, 2007
    #8
    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. JustSomeGuy
    Replies:
    1
    Views:
    344
    Mike Wahler
    Sep 20, 2003
  2. James Brown

    typedef question

    James Brown, Nov 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    458
    James Brown
    Nov 16, 2005
  3. Tony Johansson
    Replies:
    7
    Views:
    357
    Victor Bazarov
    Aug 16, 2005
  4. Alex

    typedef question

    Alex, Nov 28, 2005, in forum: C++
    Replies:
    6
    Views:
    479
    Alex O.
    Dec 2, 2005
  5. oor
    Replies:
    0
    Views:
    1,398
Loading...

Share This Page