Home > Software engineering >  Typedef in C, but a different way
Typedef in C, but a different way

Time:02-14

I am aware of the standard way to define a structure in C. But, in analogy with this:

// <typedef structureName alias>
typedef unsigned long int u_int32

I was expecting this to work too:

struct myStr {char name[100]; float avgProb; int severity;};
typedef myStr ms;
myStr ms = {name:"hell", avgProb:50.0, severity:5,};
printf("myR %f", ms.avgProb);

Is it just not possible to do it or I am missing something?

CodePudding user response:

Is it just not possible to do it or I am missing something?

You simply need to learn C syntax and basics before coding

  struct myStr {char name[100]; float avgProb; int severity;};
  typedef struct myStr ms;
  ms m = {.name = "hell", .avgProb = 50.0, .severity = 5,};
  printf("myR %f", m.avgProb);

Or simply

typedef struct 
{
    char name[100]; 
    float avgProb; 
    int severity;
}myStr;

  myStr m = {.name = "hell", .avgProb = 50.0, .severity = 5,};
  printf("myR %f", m.avgProb); 

Some good resources: The Definitive C Book Guide and List

CodePudding user response:

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;};

CodePudding user response:

struct myStr {...}; doesn't define myStr as a type in C (unlike in C ). It defines struct myStr as a type. If you want myStr to be a typedef for the defined struct myStr type, you need to say that

typedef struct myStr myStr;

If you always want such a typedef, you can create a macro for it:

#define TYPEDEFED_STRUCT(NAME) typedef struct NAME NAME; struct NAME
TYPEDEFED_STRUCT(myStr){ ... }

This behavior is due to how C separates normal and tag namespaces.

As a consequence of that, you can have both a function named e.g., stat and a structure tagged stat (struct stat) and they don't conflict. (Unix has a function named stat whose prototype is int stat(char const*, struct stat*);).

Interestingly since standardization, the tag namespace is shared between struct tags, union tags, and enum tags, so once you have struct stat, then even though you can have int stat();, defining a union stat or an enum stat would create conflicts with the struct stat (as theres's only one namespace for structs, unions, and enums).

CodePudding user response:

struct myStr {...};

This defines a struct and also a tag name for it. These tags have their own namespace, but they must be always preceded by struct.

struct my {...};

therefore is enough; as in sizeof(struct my).

For the typedef:

typedef struct my myStr;

Now myStr is of struct my type. I would prefer my_t, even though the _t is reserved.

Both at once is also possible:

typedef struct my {...} myStr;

Without typedef, myStr would be a struct my itself. With, it defines a new name for that type.

CodePudding user response:

You declared type specifier struct myStr.

struct myStr {char name[100]; float avgProb; int severity;};

Thus in this typedef declaration

typedef myStr ms;

the name myStr is undefined. To refer to the name myStr you need to precede it with the keyword struct like struct myStr.

You have to write

typedef struct myStr ms;

So now ms becomes a type specifier that is an alias for the type specifier struct myStr..

Alternatively you may rewrite the typedef declaration also the following way

struct myStr typedef me;

This declaration and the initializer list are incorrect.

myStr ms = {name:"hell", avgProb:50.0, severity:5,};

You need to write for example

ms s = { .name = "hell", .avgProb = 50.0, .severity = 5,};

or

ms s = { "hell", 50.0, 5,};

Pay attention to that the last comma though may be present but is redundant.

If you wanted to have this declaration

myStr ms = { .name = "hell", .avgProb = 50.0, .severity = 5,};

then the typedef declaration should look like

typedef struct myStr myStr;

or

struct myStr typedef myStr;
  • Related