Typedef-ing structs

K

Kaz Kylheku

Hi, every1 (quick question):

Is this valid C89 code?

I was doing a problem from a previous programming competition and I
ridded the program of every error except for one which I believe is
associated with the following:

typedef struct something {
int variable;
} lots[100];

Just a guess; you might have run into trouble by creating a typedef
name for an array type.
Why isn't this allowed? I should probably create a tiny program
containing this code and compile it b4 I ask though.

Arrays are not first class objects in C, and typedef is only an aliasing
facility; it can only give a name to an existing type, not create a new
abstraction.

So after the above typedef, the identifier ``lots'' represents a type:
an array of 100 elements of ``struct something''.

But this won't work:


lots give_me_lots(void)
{
lots l;

/* fill in l[0].variable through l[99].variable */

return l;
}

You cannot return arrays from functions in C. This calls for a diagnostic
message.

And the following will appear to be passing lots by value, but
in fact the argument l is a pointer to the first element,
allowing the function to modify the caller's object:

// sneaky: looks like regular C call-by-value;
// no [] syntax in sight to give it away!

void work_with_lots(lots l)
{
l[0].value = 42; // ha!
}

You also might have possibly run into this. Here, l is a pointer
to an aray:

void initialize_lots(lots *l)
{
int i;

for (i = 0; i < 100; i++) {
/* Awkward syntax: dereference pointer to arrray
in parentheses to refer to the array, then
apply array indexing. */
(*l) = i;
}
}

So all in all, typedef names for arrays, unless they are just used as
intermediates for defining another type, are probably going to bring
confusion into your program.
 
G

Guest

I was doing a problem from a previous programming competition and I
ridded the program of every error except for one which I believe is
associated with the following:

"which I belive is associated" is often a dodgy construct.
Post a complete compilable (or as close to compling as you can get)
program.

typedef struct something {
    int variable;

} lots[100];

Why isn't this allowed?

it *is* allowed but it may not do what you think it does.
What do you think let is?
I should probably create a tiny program
containing this code and compile it b4 I ask though.

yep
 
J

James Kuyper

CBFalconer said:
<< letters with 'struct { int v; int weight; }' ?

Where does he have, or mention, either a typedef or a macro?

He invoked the idea of macros, though not the name, by incorrectly
talking about that typedef as if it did simple replacement of the
identifier 'edge' with the full struct definition.

'edge' is a typedef. It is mentioned, and even defined, in Albert's
original message. I know that you are incapable of reading or
remembering previous messages; I appreciate that you're operating under
a handicap, but I think there's a limit to the amount of accommodation
you can reasonably expect those of us with decent newsreaders and
adequate memory capacity to do to make things easier for you.
 
R

Richard Tobin

<< letters with 'struct { int v; int weight; }' ?
No. A macro is replaced before compilation. Typedef creates a
synonym that is a proper type.
[/QUOTE]
Where does he have, or mention, either a typedef or a macro?

Any mechanism for replacing source text, other than a macro, would
be off-topic.

-- Richard
 
G

Guest

I was doing a problem from a previous programming competition and I
ridded the program of every error except for one which I believe is
associated with the following:

"which I belive is associated" is often a dodgy construct.
Post a complete compilable (or as close to compling as you can get)
program.
typedef struct something {
    int variable;
} lots[100];
Why isn't this allowed?

it *is* allowed but it may not do what you think it does.
What do you think let is?

that would be
"What do you think *lots* is?"
 
C

CBFalconer

Nelu said:
First post:

<OP>
typedef struct {
int v; /* neighboring vertex */
int weight; /* edge weight */
} edge;
</OP>

and, then, before my post, as a reply to Ian Collins' follow-up:

The point is that my post quoted ALL of your post. It made no
sense. You need to quote enough so that your message stands by
itself, is understandable, and complete. Usenet is a medium that
offers no tranmission guarantees, just best efforts. Between that,
and usage patterns, there is no reason to assume that your readers
can, or have ever, seen any other messages in the thread.

People who fail to understand this are misusing Usenet. I may be
hard to understand at times because I will make posts (like mine
above) that just point out the failing, and not the reasons for
that being a failing.
 
I

Ian Collins

CBFalconer said:
The point is that my post quoted ALL of your post. It made no
sense. You need to quote enough so that your message stands by
itself, is understandable, and complete.

And you need to learn to read threads before posting pompous nonsense.
Everyone else who read the message understood the context.
 
J

jameskuyper

CBFalconer said:
The point is that my post quoted ALL of your post. It made no
sense. You need to quote enough so that your message stands by
itself, is understandable, and complete.

That's an unreasonable criterion. in general, nothing less than
quoting 100% of all previous messages would be sufficient to allow a
message stand completely on it's own. To cover your specific objection
in this case, he would have had to quote material going all the way
back to the very first message on this thread. That would be excessive
quoting, in my opinion. Any reasonably competent C programmer would
have been able to guess 'edge' was a typedef just from context of the
message itself; other sources are needed only to confirm that guess.
Exactly how much quoting is needed is a judgment call, and I think
that reasonable judgments were made in this case. Not the same
judgments I would have made, but reasonable ones.
... Usenet is a medium that
offers no tranmission guarantees, just best efforts. Between that,
and usage patterns, there is no reason to assume that your readers
can, or have ever, seen any other messages in the thread.

The reality is that most modern newsreaders will display multiple
previous messages, and most news servers will retain a reasonable
number of previous messages, and if you suspect you've missed one, you
can always check at groups.google.com (as well as dozens of other
substantially less popular and less comprehensive archives).
If you're unwilling to make such assumptions, you shouldn't be using
usenet, because refusing to make such assumptions will lead you to
perform excessive quoting.
 
K

Keith Thompson

CBFalconer said:
The point is that my post quoted ALL of your post.
[...

Then *make that point*. Even if you hadn't seen the previous articles
in the thread, it should have been obvious that the article was
referring, at least indirectly, to previous messages in the thread.

Yo have a habit of making points about topicality and context by
*pretending* not to know what's being talked about (the "stat"
function doesn't exist, or nobody mentioned a typedef). In most
cases, I think you know the context perfectly well. And every time
you do that, *it doesn't work*.

Try a different approach.
 
A

Antoninus Twink

CBFalconer said:
[bullshit snipped]

Why anyone would work so hard to trash their own reputation is just
beyond me.

Do you think the world would be a better place if everyone was like you,
and worked hard to trash other people's reputations instead?
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top