3

I wanted to understand why with this struct definition, we can directly use the name event to assign values to structure members. I have seen other definitions, where struct keyword is used to assign values.

struct {
    int eventNum;
    int eventType;
} event;

void function() {
    event.eventNum = 10; 
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Smiley7
  • 185
  • 1
  • 3
  • 10

3 Answers3

4
struct Name {int stuff; int data;} variable;

It's just the same thing as, for example, int variable;, so, this is an ordinary variable, but with a complex, or, as pointed out in the comments, a derived type.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • `struct Name {int stuff;} variable;` is not exactly the same as `struct {int stuff;} variable;` as the second one is an incomplete struct variable declaration – 0___________ Aug 13 '17 at 19:41
  • 1
    @PeterJ, right, but I don't think it's much of a deal in this case. – ForceBru Aug 13 '17 at 19:44
  • I think it does - see how many questions people ask about the error "Incomplete struct ....." – 0___________ Aug 13 '17 at 19:45
  • 1
    @PeterJ, there are 914 questions about this under the [tag:c] tag. And more than a _hundred thousand_ only about `struct`s. So, that's around 0.9% of all the questions about only `structs`. Not much of a deal, to my mind... Yet this is definitely important. – ForceBru Aug 13 '17 at 19:50
  • "but with a complex type"-- it would be more accurate to call this a derived type. – ad absurdum Aug 13 '17 at 19:51
  • 1
    @PeterJ, if you tag in [tag:c++] (because it also has a notion of `struct`), you'll get 1785 questions about incomplete struct / 219004 questions about `struct`s = _0.815%_. – ForceBru Aug 13 '17 at 19:58
  • 3
    @PeterJ: the structure type in `struct { int stuff; } variable;` is complete once the `}` is reached. It is simply of a nameless type; no other variable of the same type can be created because there is no tag to allow the type to be named — though you might be able to create compatible types in other translation units (that bit of the standard is hard reading). – Jonathan Leffler Aug 13 '17 at 20:06
4

In your code fragment, event is an instance of an unnamed structure, an uninitialized global variable. At global scope, uninitialized objects have all members initialized to the zero value for their type.

The function function() can use the event name to refer to this object and assign a value to one of its members: event.eventNum = 10;.

You may have seen initialized structure definitions like this:

struct {
    int eventNum;
    int eventType;
} event = { 10, 0 };

Or C99 specific initalizers like this:

struct {
    int eventNum;
    int eventType;
} event = { .eventNum = 10 };

These definitions can occur at global or local scope and define an initialized object event.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
-1

you declare the global variable event which type is a "tagless" or "incomplete" (this is the question for the language lawyers how to call it properly) structure struct { int ventNum; int eventType; }. As it is the global variable, it is visible in the scope of entire program including your function

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 3
    This is not an incomplete type. The Standard defines incomplete types as ["lacking sufficient information to determine the size of objects of that type".](http://port70.net/~nsz/c/c11/n1570.html#6.2.5p1) – ad absurdum Aug 13 '17 at 19:58
  • So how to call it properly. I use (and many people I know or work with) nameless as derived is ambiguous a bit I think - not giving exact picture of it. – 0___________ Aug 13 '17 at 20:06
  • @PeterJ, all structs are derived, yet not all of them are "nameless". – ForceBru Aug 13 '17 at 20:07
  • 1
    The standard doesn't give a short-form name for structure types defined without a tag. For example, §6.7.2.3 **Tags** uses the circumlocution: _Two declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types. Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type._ Using the term tag-less or name-less is not unreasonable. The term 'anonymous' is not appropriate in general; it has a specific meaning. In `typedef struct { … } Name;`, the type has a name but no tag. – Jonathan Leffler Aug 13 '17 at 20:27
  • 1
    so tagless will be the most appropriate – 0___________ Aug 13 '17 at 20:29
  • 1
    I agree that in the absence of anything more official, "tagless" is the best alternative because it is succinct and accurate. And I'd spell it without the hyphen I used originally, despite the spell-checker's insistence that it needs a squiggly red underline. – Jonathan Leffler Aug 13 '17 at 20:59
  • @Jonathan Leffler You are the very first here, who understands what I mean :). Here in the UK "nameless" is quite common (local IT tribes jargon) and everyone understands what it means – 0___________ Aug 13 '17 at 21:03
  • It could be something to do with the fact that I migrated to the US from the UK twenty-something years ago. :D – Jonathan Leffler Aug 13 '17 at 21:05
  • @jonathan: Personally, I think "untagged" is better than "tagless", not because of the spell-checker's admonition, but because I recognise it as an not uncommon word. (Tagless is not incorrect, imho, since "-less" is productive, but I don't think it is common.) – rici Aug 15 '17 at 15:40
  • @rici: Interesting alternative — perfectly feasible. I might quibble that 'untagged' has connotations of 'having had its tag removed', but that's pretty borderline and mostly just quibbling (NIH syndrome?). 'Tis a pity that the standard manages to do without an official term for a 'struct type without a tag' (other than the special case of 'anonymous struct' which has special rules and requirements and isn't under discussion here). – Jonathan Leffler Aug 15 '17 at 15:47
  • @Jonathan Leffler Some guys here were giving me DVs as quick as they only could for using "nameless" (I was explaining what I mean). So everything we discuss here is a profanity, as for many The Standard is like The Bible. And any term, word or thought which is not in The Standard is the heresy. And from the past you know what should happen to the heretic. – 0___________ Aug 15 '17 at 16:03
  • PeterJ: don't exaggerate. This answer received exactly one downvote and there is no evidence that anyone rushed to do it. While "nameless" is arguably imprecise, your insistence on the incorrect use of "incomplete" would, IMHO, be sufficient to justify a downvote. But the presence or absence of a tag is completely irrelevant to the question asked, so your answer is not a useful response to the actual question. – rici Aug 15 '17 at 19:24