473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

struct definition...

Hi all,

What is the difference between:

typedef struct
{
...
} MyS1;

....and...

typedef struct MyS2
{
...
} MyS2;

Could both of the cases be instantiated as follows?

MyS1 inst1;
MyS2 inst2;

Also, is the following possible...

struct MyS3{ ... };

MyS3 inst3;

Kind regards,

Werner

Aug 29 '06 #1
9 7175
werasm wrote:
>
What is the difference between:

typedef struct
{
...
} MyS1;

...and...

typedef struct MyS2
{
...
} MyS2;

Could both of the cases be instantiated as follows?

MyS1 inst1;
MyS2 inst2;
Yes. However you will not be able to instantiate a MyS1 with the
actual name, as you can with struct MyS2 in:

struct MyS2 foo;

because MyS1 is the only name available for the otherwise anonymous
struct.
>
Also, is the following possible...

struct MyS3{ ... };

MyS3 inst3;
No. The name is "struct MyS3".

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!

Aug 29 '06 #2
struct doesn't create a new type which can be used like "int" or
"float". If you just do

struct foo {
...
};

Then anytime you want to declare a variable of that type, you'll need
to write

struct foo a_variable;

Instead of just writing

foo a_variable;

Think of struct as a separate namespace in which foo lives.

werasm wrote:
Hi all,

What is the difference between:

typedef struct
{
...
} MyS1;
The struct you defined here can only be used like this:

MyS1 a_variable;

This is not valid, because you defined an "anonymous" struct:

struct MyS1 a_variable;
>
...and...

typedef struct MyS2
{
...
} MyS2;
Because you've also written the name of the struct after the "struct"
keyword, you can now also use it like this:

struct MyS2 a_variable;

As well as:

MyS2 a_variable;
>
Could both of the cases be instantiated as follows?

MyS1 inst1;
MyS2 inst2;
Yes
>
Also, is the following possible...

struct MyS3{ ... };

MyS3 inst3;
That's wrong. Only built-in types and typedef'd types can be used that
way. You need to do this:

struct MyS3 a_variable;
>
Kind regards,

Werner
To help you understand what typedef does, I've included a passage from
The C Book [http://publications.gbdirect.co.uk/c_book/]

"The general rule with the use of typedef is to write out a declaration
as if you were declaring variables of the types that you want. Where a
declaration would have introduced names with particular types,
prefixing the whole thing with typedef means that, instead of getting
variables declared, you declare new type names instead. Those new type
names can then be used as the prefix to the declaration of variables of
the new type."

Have fun!

TJ

Aug 29 '06 #3
My goodness... Google Groups chopped up my reply... Here's what I
actually wrote:

----- Start real reply -----

struct doesn't create a new type which can be used like "int" or
"float". If you just do

struct foo {
...
};

Then anytime you want to declare a variable of that type, you'll need
to write

struct foo a_variable;

Instead of just writing

foo a_variable;

Think of struct as a separate namespace in which foo lives.

werasm wrote:
Hi all,

What is the difference between:

typedef struct
{
...
} MyS1;
The struct you defined here can only be used like this:

MyS1 a_variable;

This is not valid, because you defined an "anonymous" struct:

struct MyS1 a_variable;
>
...and...

typedef struct MyS2
{
...
} MyS2;
Because you've also written the name of the struct after the "struct"
keyword, you can now also use it like this:

struct MyS2 a_variable;

As well as:

MyS2 a_variable;
>
Could both of the cases be instantiated as follows?

MyS1 inst1;
MyS2 inst2;
Yes
>
Also, is the following possible...

struct MyS3{ ... };

MyS3 inst3;
That's wrong. Only built-in types and typedef'd types can be used that
way. You need to do this:

struct MyS3 a_variable;
>
Kind regards,

Werner
To help you understand what typedef does, I've included a passage from
The C Book [http://publications.gbdirect.co.uk/c_book/]

"The general rule with the use of typedef is to write out a declaration
as if you were declaring variables of the types that you want. Where a
declaration would have introduced names with particular types,
prefixing the whole thing with typedef means that, instead of getting
variables declared, you declare new type names instead. Those new type
names can then be used as the prefix to the declaration of variables of
the new type."

Have fun!

TJ

Aug 29 '06 #4

tjay wrote:
Werner
>
To help you understand what typedef does, I've included a passage from
The C Book [http://publications.gbdirect.co.uk/c_book/]

"The general rule with the use of typedef is to write out a declaration
as if you were declaring variables of the types that you want. Where a
declaration would have introduced names with particular types,
prefixing the whole thing with typedef means that, instead of getting
variables declared, you declare new type names instead. Those new type
names can then be used as the prefix to the declaration of variables of
the new type."

Have fun!

TJ
Thank you, I'll look at the book - to summarize what you've said:

Therefore...
(1)
struct ms{ };
creates a structure identified by "ms" instantiated as follow:
struct ms inst;

(2)
typedef struct ms{ } ms;
creates a named type "ms" instantiated as follows:
ms inst;
....but can also be identified by "ms" in
struct ms inst;

(3)
typedef struct { } ms;
creates an anonymous struct that can be instantiated as follows:
ms inst;
BTW, is this instantiable in many scopes?:

void foo1(void){ ms inst; }
void foo2(void){ ms inst; }

(4)
typedef struct ms{ };

.... is this the same as ...

struct ms{ };

The reason behind my questions (I'm a C++ programmer, BTW), is that we
want to share some C code. I therefore want to create the equivalent of
a C++ structure in C (apart from the structure not having member
functions).

//C++

struct A{ }; /*instantiatable by A a;*/

What is the equivalent in C?

typedef struct A{} A; /*?*/

Kind of between languages, hope I'm OT :-).

Eventually we want to do this (to define structures in one place and
use in C and CPP code:

/* c_struct.h */

typedef struct A{ ... }A;

//cpp_struct.hpp
struct A_cpp : public A{ ... };

therefore making all in A qualifiable.
Regards,

Werner

Aug 29 '06 #5
Thank you, I'll look at the book - to summarize what you've said:
>
Therefore...
(1)
struct ms{ };
creates a structure identified by "ms" instantiated as follow:
struct ms inst;
Right.
(2)
typedef struct ms{ } ms;
creates a named type "ms" instantiated as follows:
ms inst;
...but can also be identified by "ms" in
struct ms inst;
Right.
(3)
typedef struct { } ms;
creates an anonymous struct that can be instantiated as follows:
ms inst;
Right again.
BTW, is this instantiable in many scopes?:

void foo1(void){ ms inst; }
void foo2(void){ ms inst; }
Yes if you defined the stuct at the top level. If it was defined in
foo1, then foo2 would not be able to access it.
(4)
typedef struct ms{ };

... is this the same as ...

struct ms{ };
Not sure about this. I think

typedef struct ms { };

might not be legal, as it does not end up in a name for the typedef.

typedef struct ms { } /* missing name here */ ;
The reason behind my questions (I'm a C++ programmer, BTW), is that we
want to share some C code. I therefore want to create the equivalent of
a C++ structure in C (apart from the structure not having member
functions).

//C++

struct A{ }; /*instantiatable by A a;*/

What is the equivalent in C?

typedef struct A{} A; /*?*/
I think this is correct.
This way, you can do both types of declarations in C and C++ code:

struct A foo;
A bar;
>
Kind of between languages, hope I'm OT :-).

Eventually we want to do this (to define structures in one place and
use in C and CPP code:

/* c_struct.h */

typedef struct A{ ... }A;

//cpp_struct.hpp
struct A_cpp : public A{ ... };

therefore making all in A qualifiable.
I didn't even know you could inherit from a struct. You need someone
else's opinion on this :p

Cheers,

TJ

Aug 29 '06 #6

tjay wrote:
I didn't even know you could inherit from a struct. You need someone
else's opinion on this :p
This is OT, but as it answers your q. In C++ the only difference
between a struct and a class is is its default access specifiers e.g.

//Note: C++

struct A
{
//All members are public..., if you inherit
// from, defaults to public inheritance
};

class A
{
//All members private... defaults to private inheritance.
};

Thanks again for your help.

Kind regards,

Werner
>
Cheers,

TJ
Aug 29 '06 #7
In article <11*********************@p79g2000cwp.googlegroups. com>
werasm <w_*****@telkomsa.netwrote:
>struct ms{ };
creates a structure identified by "ms" instantiated as follow:
struct ms inst;
Yes (although there must be contents within the {}s, in C; C++ differs
here).
>typedef struct ms{ } ms;
creates a named type "ms" instantiated as follows:
ms inst;
...but can also be identified by "ms" in
struct ms inst;
Essentially. However, note that it is the "struct" keyword that
creates the type, which is named "struct ms". The "typedef",
whose syntax includes things like:

typedef existing_type alias_1, alias_2, alias_3;

then creates however many aliases you list for the existing type.
The syntax for "typedef" is identical to that for ordinary variable
declarations, so:

typedef int tA, tB[10], *tC, tD(void);

defines four aliases: tA means "int", tB means "int [10]",
tC means "int *", and tD means "int (void)". This last one is
a function type, and can be used to declare functions but not
to define them:

tD foo; /* equivalent to int foo(void); */
tD foo { return 42; } /* WRONG; diagnostic required */
>typedef struct { } ms;
creates an anonymous struct that can be instantiated as follows:
ms inst;
Yes: as before, the "struct" creates the type, then the subsequent
"typedef" gives it an alias.
>BTW, is this instantiable in many scopes?:

void foo1(void){ ms inst; }
void foo2(void){ ms inst; }
Yes. Typedef-ed identifiers obey normal scoping rules.
>typedef struct ms{ };

... is this the same as ...

struct ms{ };
It is analagous to:

int ;

which requires a diagnostic.
>The reason behind my questions (I'm a C++ programmer, BTW), is that we
want to share some C code. I therefore want to create the equivalent of
a C++ structure in C (apart from the structure not having member
functions).
There are a number of subtle differences here between C and C++,
but if you want your C type-names to "work like" C++'s, do:

/* optional: struct ms; */
typedef struct ms ms;
struct ms {
... contents ...
};

The initial "typedef" contains the "struct ms" part, which --
assuming "struct ms" has not yet been defined elsewhere -- creates
the type as an "incomplete type". The "typedef" part then gives
it the alias. Having created both the incomplete type *and* the
alias, you can now go on to complete the type, *and* use the alias:

typedef struct ms ms;
struct ms {
ms *next;
... additional elements ...
};

See also <http://web.torek.net/torek/c/types2.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 29 '06 #8
Chris Torek wrote:
In article <11*********************@p79g2000cwp.googlegroups. com>
werasm <w_*****@telkomsa.netwrote:
typedef struct ms{ };

... is this the same as ...

struct ms{ };
Yes, it is.
It is analagous to:

int ;

which requires a diagnostic.
No, it isn't, and no diagnostic is required. The presence of a storage
class specifier ("typedef") does not imply any requirement for a
declarator.

typedef struct ms { };

(after adding members) is well-defined, and "typedef" is effectively
ignored. (I'll admit it's pointless, though.)

Aug 30 '06 #9
>Chris Torek wrote:
[an "empty" typedef, which in this case also included a non-empty
struct definition]
>... is analagous to:
int ;
which requires a diagnostic.
In article <11**********************@74g2000cwt.googlegroups. com>
Harald van Dijk <tr*****@gmail.comwrote:
>No, it isn't, and no diagnostic is required. The presence of a storage
class specifier ("typedef") does not imply any requirement for a
declarator.

typedef struct ms { };

(after adding members) is well-defined, and "typedef" is effectively
ignored. (I'll admit it's pointless, though.)
Oops. Indeed, no diagnostic is required in this case. I was
mentally mis-applying the rule that says:

A declaration shall declare at least a declarator (excluding
the parameters of a function or the members of a structure or
union), a tag, or the members of an enumeration.

Hence:

int; /* diagnostic required */
static int; /* diagnostic still required */
typedef; /* diagnostic required */

but:

typedef struct foo; /* diagnostic not required */
static enum { BAR }; /* diagnostic not required */

because the last two declare a tag (on the line with typedef) and
members of an enumeration (on the line with static).

(I would be unsurprised to see warnings for both of them, about
useless storage-class-specifier keywords, from any decent compiler.
But then warnings are always allowed.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
2
by: Neil McPhail | last post by:
I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete C Reference. I appreciate this may not have been the best idea, so no need to point this out! On page 546 there is a...
5
by: Ninan | last post by:
given a struct typedef struct { int a; int b; } Logstruct; How can I add a new member to the struct typedef struct {
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
10
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
2
by: jordanp | last post by:
Hello, I'm having a little trouble here and I'm hoping that somebody might be able to help me out (win32 console program). First off, I know that I can use class function inside of my struct as...
14
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.