op
Thank you
I am using C++, the example above is the only one I found (without recherching too much I musr admit).
But as I figured, I got the gist of what I was asking (conceptually). Thanks again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
is the declaration of a new type, named Strong. After that, you can instantiate that kind of objects:
Stong st1;
Strong st2;
etc...
>> typedef struct Strong {...};
This is a nonsense and here useless. To be usable You must use some as:
typedef struct Strong {...} ST;
Now ST is an alias to Strong, and you can say
ST st1;
ST st2;
typedef uses to be used to simplify complex expresions, and mostly to locate certain concrete references in a single point (where the typedef is declared), so that the possible later changes require to change a single code line (in my opinion, maybe be this the most important reason for the existence of this specificator). For example, let us suppose that we need a variable with 32 bits and we are using C++ Builder, in which int has 32 bits exactly; instead of it we use the expression:
typedef int INT32;
in the successive code, for all the references we use INT32 instead of int. For example:
INT32 x, y, z;
If has to carry the code to a machine or a compiler in which int was smaller and the 32 bits demanded a long int, we would have to change only the typedef:
typedef long int INT32; / / All the other references stay
...
INT32 x, y, z; / / Ok.
Old newbie
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
and simply allows <type_name> to be used as an alias for "struct <structure_tag>".
The construct is only useful in C since in C++ the <structure_tag> is a type name in any case; the struct keyword is optional in C++ when instantiating structure objects. It serves to make C code look a little less cluttered and more like C++ (which has plenty of other clutter of its own!).
Oh... I just read Old Newbie's post, and he said more or less the same thing (before going off teh point a little). Never mind, I'll let it stand, having it explained twice might even help! ;-)
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OP
To present a more accurate question, this what I have:
include <stdio.h>
typedef struct mystructtag
{
int i;
double f;
} mystruct;
int main()
{
mystruct ms;
ms.i = 10;
ms.f = 0.99;
printf ("%d %f\n", ms.i, ms.f;
system (pause);
return 0;
}
If I understand, this piece of code means that mystruct becomes the name of the struct and it defines ms.i and ms,f as object of the struct.
What is the advantage(s) of doing this, rather that calling the struct mystruct to begin with?
There must be a reason, invisible to me from such a simple code.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
>> What is the advantage(s) of doing this, rather that calling the struct mystruct to begin with?
None in this case
>> There must be a reason, invisible to me from such a simple code
Also invisible for me. In fact there are not reason.
As I mentioned before, there are only two reasons for use that: "to simplify complex expresions, and mostly to locate certain concrete references in a single point". I've explained the former, now I will try to explain the first:
Suppose you need introduce in your's code a lot of pointers to function without parameters and returning an array of five pointers to function... Some like this:
long ((( fpt1) ()) [5])();
long ((( fpt2) ()) [5])();
long (((* fpt3) ()) [5])();
...
Etc.
Is simpler and crisply to make:
typedef long (((*FPTR)())[5])();
FPTR fpt1, fpt2, fpt3, ...;
And... Yes, typedef can by used whit only one argument ;-))
Old Newbie
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
>> What is the advantage(s) of doing this, rather that calling the struct mystruct to begin with?
If you didn't do this, and it is C code (not C++), then you wouldn't be able to write the following in main:
mystruct ms;
Without the typedef it would have to be
struct mystruct ms;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-06-29
The advantage is that it is merely that it is less clutter and easier to read and maintain.
As I said it is unnecessary if you are using C++ compilation where:
struct mystruct
{
...
} ;
int main()
{
mystruct ms;
...
}
is just fine.
In C self-referring structures (such as link list nodes) must use the struct form in the reference as the type is not defined until the end of the statement:
What is the difference between:
typedef struct Strong {...}
and
struct Strong {...}
????
op
Thank you
I am using C++, the example above is the only one I found (without recherching too much I musr admit).
But as I figured, I got the gist of what I was asking (conceptually). Thanks again.
>> struct Strong {...};
is the declaration of a new type, named Strong. After that, you can instantiate that kind of objects:
Stong st1;
Strong st2;
etc...
>> typedef struct Strong {...};
This is a nonsense and here useless. To be usable You must use some as:
typedef struct Strong {...} ST;
Now ST is an alias to Strong, and you can say
ST st1;
ST st2;
typedef uses to be used to simplify complex expresions, and mostly to locate certain concrete references in a single point (where the typedef is declared), so that the possible later changes require to change a single code line (in my opinion, maybe be this the most important reason for the existence of this specificator). For example, let us suppose that we need a variable with 32 bits and we are using C++ Builder, in which int has 32 bits exactly; instead of it we use the expression:
typedef int INT32;
in the successive code, for all the references we use INT32 instead of int. For example:
INT32 x, y, z;
If has to carry the code to a machine or a compiler in which int was smaller and the 32 bits demanded a long int, we would have to change only the typedef:
typedef long int INT32; / / All the other references stay
...
INT32 x, y, z; / / Ok.
Old newbie
The syntax you posted is incomplete:
The first should be of the form:
struct <structure_tag> { <structure members> } <type_name> ;
and simply allows <type_name> to be used as an alias for "struct <structure_tag>".
The construct is only useful in C since in C++ the <structure_tag> is a type name in any case; the struct keyword is optional in C++ when instantiating structure objects. It serves to make C code look a little less cluttered and more like C++ (which has plenty of other clutter of its own!).
Oh... I just read Old Newbie's post, and he said more or less the same thing (before going off teh point a little). Never mind, I'll let it stand, having it explained twice might even help! ;-)
Clifford
OP
Thanks a lot!
como se hacen?
OP
To present a more accurate question, this what I have:
include <stdio.h>
typedef struct mystructtag
{
int i;
double f;
} mystruct;
{
mystruct ms;
ms.i = 10;
ms.f = 0.99;
}
If I understand, this piece of code means that mystruct becomes the name of the struct and it defines ms.i and ms,f as object of the struct.
What is the advantage(s) of doing this, rather that calling the struct mystruct to begin with?
There must be a reason, invisible to me from such a simple code.
>> What is the advantage(s) of doing this, rather that calling the struct mystruct to begin with?
None in this case
>> There must be a reason, invisible to me from such a simple code
Also invisible for me. In fact there are not reason.
As I mentioned before, there are only two reasons for use that: "to simplify complex expresions, and mostly to locate certain concrete references in a single point". I've explained the former, now I will try to explain the first:
Suppose you need introduce in your's code a lot of pointers to function without parameters and returning an array of five pointers to function... Some like this:
long ((( fpt1) ()) [5])();
long ((( fpt2) ()) [5])();
long (((* fpt3) ()) [5])();
...
Etc.
Is simpler and crisply to make:
typedef long (((*FPTR)())[5])();
FPTR fpt1, fpt2, fpt3, ...;
And... Yes, typedef can by used whit only one argument ;-))
Old Newbie
>> What is the advantage(s) of doing this, rather that calling the struct mystruct to begin with?
If you didn't do this, and it is C code (not C++), then you wouldn't be able to write the following in main:
mystruct ms;
Without the typedef it would have to be
struct mystruct ms;
The advantage is that it is merely that it is less clutter and easier to read and maintain.
As I said it is unnecessary if you are using C++ compilation where:
struct mystruct
{
...
} ;
int main()
{
mystruct ms;
...
}
is just fine.
In C self-referring structures (such as link list nodes) must use the struct form in the reference as the type is not defined until the end of the statement:
typedef struct mystructtag
{
struct mystructtag* link ;
...
} mystruct ;
whereas in C++:
struct mystruct
{
struct mystruct* link ;
...
} ;
is valid.
Make it easy on your self, and use C++ compilation (even if it is non-OO code).
Clifford