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 9 7206
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********@yah oo.com) (cb********@mai neline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.netUSE maineline address!
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
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
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
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
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
In article <11************ *********@p79g2 000cwp.googlegr oups.com>
werasm <w_*****@telkom sa.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.
Chris Torek wrote:
In article <11************ *********@p79g2 000cwp.googlegr oups.com>
werasm <w_*****@telkom sa.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.)
>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************ **********@74g2 000cwt.googlegr oups.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 options. I can get this to worksuccessfully, but only when I'm NOT dealing with arrays in thestructure definition.
For example: In C, you are able to simply read a binary file intoa structure as follows:
...
|
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 mailing list program with a struct defined thus:
struct address {
char name;
char street;
char city;
|
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 {
|
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
|
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;
};
| |
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 a struct object...but my
issue that I'm having is that my class function is set up so that it
sets 3 variables...
Example of my class object (has the getter(), and setter() functions
below this, but I won't display that here):
|
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 whateverkeithsaysfor99compliance
#define whateverkeithsays for99compliance
int main(void
{
struct foo
|
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 structure is defined in the header(.h file) as shown below
struct validationResult
{
|
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) {
struct A { int _; } a;
((T*)0)(&a);
|
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 method to declare my structs, but in many open
source projects (such as libxml2), the third method is used. I am just
curious about the difference.. Would somebody tell me which method is
recommended, and why? Thanks in advance.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |