typedef question

L

Logan Lee

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?
 
B

Ben Bacarisse

Logan Lee said:
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.
 
R

Richard Bos

Logan Lee said:
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
 
E

Eric Sosman

Logan said:
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.
 
K

Keith Thompson

Eric Sosman said:
No, as your compiler would have told you had you asked it.


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.
 
E

Eric Sosman

Keith said:
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.
 
K

Keith Thompson

Eric Sosman said:
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.
 
H

Harald van Dijk

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 */ }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top