Sorry for interrupt,because my mother language is not English,it maybe a little confused to read the

Z

Zhang Yuan

charpter6
6.1 basic

"The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the
declaration in braces. "

I don't understand it well.
I refer to some native language(Chinese) translated from English.
just literal translation.

Will you put up an example for me to understand it?
Thank you.
Forgive me for my silly request.
 
I

Ike Naar

charpter6
6.1 basic

"The tag names this kind of structure, and can be used subsequently
as a shorthand for the part of the
declaration in braces. "

I don't understand it well.
I refer to some native language(Chinese) translated from English.
just literal translation.

Will you put up an example for me to understand it?

They probably mean that with this type definition:

struct S { int i; double d; };

the declaration

struct S x;

can be seen as a shorthand for

struct { int i; double d; } x;

Indeed, both declarations will declare a variable x that is
a struct type with an int member i and a double member d.

But there are subtle differences. For instance, with

struct S x;
struct S y;

x and y have the same type, and can be assigned to each other, like in

x = y;

but with

struct S x;
struct { int i; double d; } y;

x and y have different types, and cannot be assigned to each other:

x = y;

is not allowed.
 
Z

Zhang Yuan

They probably mean that with this type definition:

struct S { int i; double d; };

the declaration

struct S x;

can be seen as a shorthand for

struct { int i; double d; } x;

Indeed, both declarations will declare a variable x that is
a struct type with an int member i and a double member d.

But there are subtle differences. For instance, with

struct S x;
struct S y;

x and y have the same type, and can be assigned to each other, like in

x = y;

but with

struct S x;
struct { int i; double d; } y;

x and y have different types, and cannot be assigned to each other:

x = y;

is not allowed.


Thank you.Learn a lot.
 
B

Barry Schwarz

charpter6
6.1 basic

"The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the
declaration in braces. "

I don't understand it well.
I refer to some native language(Chinese) translated from English.
just literal translation.

Will you put up an example for me to understand it?
Thank you.
Forgive me for my silly request.

This is from K&R II, page 128.

The example they use is
struct point {int x; int y;};

In this case, the tag is the token "point" (without the quotes).

I think the idea they are trying to explain is that if you want to
define an object of this type, you can use
struct point p;
and if later you need to define another object of this type you can
use
struct point q;

Compare this with what happens if the structure type has no tag, as in
struct {int x; int y;};

In this case, to define an object of this type you would need
struct {int x; int y;} p;
and for a second object
struct {int x; int y;} q;

If the structure type has many members, this can be quite tiresome. It
also becomes difficult to verify that both p and q are the same type.

So the tag "point" serves as a shorter way to specify
"{int x; int y;}". This shorthand is available only for types that
have tags (unions, structures, and enums).
 
Z

Zhang Yuan

Thank you.
I understand it well now


This is from K&R II, page 128.

The example they use is
struct point {int x; int y;};

In this case, the tag is the token "point" (without the quotes).

I think the idea they are trying to explain is that if you want to
define an object of this type, you can use
struct point p;
and if later you need to define another object of this type you can
use
struct point q;

Compare this with what happens if the structure type has no tag, as in
struct {int x; int y;};

In this case, to define an object of this type you would need
struct {int x; int y;} p;
and for a second object
struct {int x; int y;} q;

If the structure type has many members, this can be quite tiresome. It
also becomes difficult to verify that both p and q are the same type.

So the tag "point" serves as a shorter way to specify
"{int x; int y;}". This shorthand is available only for types that
have tags (unions, structures, and enums).

--
Remove del for email




This is from K&R II, page 128.

The example they use is
struct point {int x; int y;};

In this case, the tag is the token "point" (without the quotes).

I think the idea they are trying to explain is that if you want to
define an object of this type, you can use
struct point p;
and if later you need to define another object of this type you can
use
struct point q;

Compare this with what happens if the structure type has no tag, as in
struct {int x; int y;};

In this case, to define an object of this type you would need
struct {int x; int y;} p;
and for a second object
struct {int x; int y;} q;

If the structure type has many members, this can be quite tiresome. It
also becomes difficult to verify that both p and q are the same type.

So the tag "point" serves as a shorter way to specify
"{int x; int y;}". This shorthand is available only for types that
have tags (unions, structures, and enums).

--
Remove del for email




This is from K&R II, page 128.

The example they use is
struct point {int x; int y;};

In this case, the tag is the token "point" (without the quotes).

I think the idea they are trying to explain is that if you want to
define an object of this type, you can use
struct point p;
and if later you need to define another object of this type you can
use
struct point q;

Compare this with what happens if the structure type has no tag, as in
struct {int x; int y;};

In this case, to define an object of this type you would need
struct {int x; int y;} p;
and for a second object
struct {int x; int y;} q;

If the structure type has many members, this can be quite tiresome. It
also becomes difficult to verify that both p and q are the same type.

So the tag "point" serves as a shorter way to specify
"{int x; int y;}". This shorthand is available only for types that
have tags (unions, structures, and enums).
 
J

Joe keane

They probably mean that with this type definition:

struct S { int i; double d; };

the declaration

struct S x;

can be seen as a shorthand for

struct { int i; double d; } x;

No, it's just wrong.

A struct defined with a tag can be referred to by the same tag, and then
it's the same type; it's probably illegal to redefine it with the same
tag, even if the member types and names are identical, and a struct with
a tag will never be the same type as an anonymous struct, and neither
will two anonymous structs be the same type.

So that's part of the problem he's having.
 
I

Ike Naar

No, it's just wrong.

A struct defined with a tag can be referred to by the same tag, and then
it's the same type; it's probably illegal to redefine it with the same
tag, even if the member types and names are identical, and a struct with
a tag will never be the same type as an anonymous struct, and neither
will two anonymous structs be the same type.

We are in agreement.
You more or less restate what was stated
in the part of my post that you snipped.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top