Tyler
Tyler

Reputation: 2639

In C, when do structure names have to be included in structure initializations and definitions?

I'm reading The C Programming Language by K&R and in the section on structures I came across these code snippets:

struct maxpt = { 320, 200 };

and

/* addpoints: add two points */
struct addpoint(struct point p1, struct point p2)
{
    p1.x += p2.x;
    p1.y += p2.y;
    return p1;
}

In the first case, it looks like it's assigning the values 320 and 200 to the members of the variable maxpt. But I noticed the name of the struct type is missing (shouldn't it be "struct struct_name maxpt = {320, 200}"? In the second case, the function return type is just "struct" and not "struct name_of_struct".

I don't get why they don't include the struct names - how does it know what particular type of structure it's dealing with? My confusion is compounded by the fact that in previous snippets they do include the structure name, such as in the return type for the following function, where it's "struct point" and not just "struct". Why do they include the name in some cases and not in others?

/* makepoint: make a point from x and y components */
struct point makepoint(int x, int y)
{
    struct point temp;
    temp.x = x;
    temp.y = y;
    return temp;
}

Upvotes: 3

Views: 373

Answers (2)

user1129665
user1129665

Reputation:

This is a typo. I have the a copy witch has the same error on page 115, should be struct point maxpt = { 320, 200 };:

A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above,

 struct point pt;

defines a variable pt which is a structure of type `struct point``. A structure can be initialized by following its definition with a list of initializers, each a constant expression, for the members:

struct maxpt = { 320, 200 };

An automatic structure may also be initialized by assignment or by calling a function that returns a structure of the right type.

on page 117:

The next step is a set of functions to do arithmetic on points. For instance,

/* addpoints: add two points */
struct addpoint(struct point p1, struct point p2)
{
    p1.x += p2.x;
    p1.y += p2.y;
    return p1;
}

Here both the arguments and the return value are structures. We incremented the components in p1 rather than using an explicit temporary variable to emphasize that structure parameters are passed by value like any others.

Upvotes: 1

Brian Campbell
Brian Campbell

Reputation: 333304

My copy of K&R includes on page 128:

struct point maxpt = { 320, 200 };

and on page 130:

struct point addpoint(struct point p1, struct point p2)
{
    p1.x += p2.x;
    p1.y += p2.y;
    return p1;
}

This is the Second Edition, 47th printing, February 2011.

If your copy of K&R includes something else, I believe it's mistaken.

Upvotes: 5

Related Questions