I was expecting this to work also:
typedef myStr ms;
Is it just not possible to do it or I am missing something?
It's possible, but what you're doing there isn't it. (In a sense you're begging the question.)
Your attempt
typedef myStr ms;
would work if myStr
were already a typedef name. But of course it's not, yet.
When you declare a structure like
struct myStr {char name[100]; float avgProb; int severity;};
its full, official name (the name on its birth certificate, so to speak) is struct myStr
.
A lot of programmers (especially if they've seen how "easy" it is in C++) wish that their structures had single names, wish that they didn't have to keep slinging that struct
keyword around. And you can certainly do that, by declaring a typedef. But, when you declare this typedef, it has to be in terms of the full, official, existing name of the type. So it's
typedef struct myStr ms;
where we can break that down as
typedef struct myStr ms;
\_____/ \__________/ \/
keyword old name new
name
You asked about C, but I suspect your thinking here has been influenced by something you may have heard abut C++.
Things are a little different in C++, and when you say
struct myStr {char name[100]; float avgProb; int severity;};
it not only declares a structure struct myStr
, it also implicitly creates a typedef at the same time. It's as if you had also said
typedef struct myStr myStr;
So now (in C++) you can either say
struct myStr struct1 = { "Fred", 1.2, 3 };
or
myStr struct1 = { "Barney", 4.5, 6 };
(And, in case you're wondering, yes: the typedef name is exactly the same as the structure name, but it turns out that's okay, and not ambiguous.)
So, in C++, you could say
typedef myStr ms;
if you wanted to give your structure a second, shorter name. Although, in C++, you wouldn't really need to do that, either, because you could just give it that shorter name to begin with:
struct ms {char name[100]; float avgProb; int severity;};