Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a struct being typedef-ed to multiple names?

Tags:

c

struct

typedef

I've recently started to study about structs and pointers but there is something I didn't fully understand about the design of a struct. I understand the declaration of the struct i.e typedef struct Alias and its content but I don't understand Get_noAllyp and *no_getOf at the end of the statement. What are they? I couldn't really find a good source either.

typedef struct  Alias {
    char    *s_a_name;
    char    **s_aliases;
    short   *s_dumr;
    int     s_get_sum;
}Get_noAllyp, *no_getOf; /*Here, I don't understand this one. 
                        Where did these two variables come from?
                        And one of them is a pointer.*/
like image 546
Volkan Güven Avatar asked May 21 '15 09:05

Volkan Güven


People also ask

How typedef is used in structure?

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.

Can typedef and struct have same name?

You can't "typedef a struct", that doesn't mean anything.

Can we typedef in declaring structure?

You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration. Typedef names can be used to improve code readability.

Should you typedef a struct?

PLEASE don't typedef structs in C, it needlessly pollutes the global namespace which is typically very polluted already in large C programs. Also, typedef'd structs without a tag name are a major cause of needless imposition of ordering relationships among header files.

Why do we use ‘typedef’ in C++?

Using ‘typedef’, we cannot create a new datatype but define a new name for already existing type. This statement tells the compiler to recognize ‘bhanu’ as another name for ‘int’. ‘bhanu’ is used to create another variable ‘a’ . ‘bhanu a ‘declares ‘a’ as a variable of type ‘int’.

What is the use of ‘typedef’ in Python?

Using ‘typedef’, we cannot create a new datatype but define a new name for already existing type. This statement tells the compiler to recognize ‘bhanu’ as another name for ‘int’. ‘bhanu’ is used to create another variable ‘a’ .

How to access the members of a structure?

As we know that – to access the structure members, we need an object of the structure – that is known as a structure variable. Structure variable declaration of simple structure declaration Syntax:

When to use a new defined type in C?

This is very used for example when working with linked lists in C The new defined type can be used just as other basic types for almost everything. Try for example to create an array of type student and see how it works. Structs can be copied or assigned but you can not compare them!


3 Answers

It defines multiple typedefs, i.e multilple "names" for the same thing, while the second is a pointer to it.

The first one Get_noAllyp is the name given for the struct, while no_getOf represents a pointer to it.

I.e, writing no_getOf is completely the same as writing Get_noAllyp * in function signatures or variable declarations.

like image 123
Mark Segal Avatar answered Sep 24 '22 08:09

Mark Segal


Here, there are two typedefs being crated in a short-hand manner. The above typedef can be broken down like

typedef struct  Alias {
    char    *s_a_name;
    char    **s_aliases;
    short   *s_dumr;
    int     s_get_sum;
}Get_noAllyp;                  

typedef struct  Alias * no_getOf;

So,

  • Get_noAllyp represents struct Alias
  • no_getOf represents struct Alias *
like image 34
Sourav Ghosh Avatar answered Sep 22 '22 08:09

Sourav Ghosh


The code:

struct Alias {
    char    *s_a_name;
    char    **s_aliases;
    short   *s_dumr;
    int     s_get_sum;
}

defines a new data type that has the name Alias and is a struct. The original design of the C language is a bit clumsy here as it requires the struct type names to be always prefixed with the struct keyword when they are used.

This means the code:

struct  Alias {
    char    *s_a_name;
    char    **s_aliases;
    short   *s_dumr;
    int     s_get_sum;
} Get_noAllyp, *no_getOf;

declares the variable Get_noAllyp of type struct Alias and the variable no_getOf of type pointer to struct Alias.

By placing the typedef keyword in front, the identifiers Get_noAllyp and no_getOf become types (and not variables).

Get_noAllyp is the same as struct Alias and no_getOf is the same as struct Alias * (i.e. a pointer to a struct Alias`).

Now you can write:

struct Alias x;
struct Alias *y;

or

Get_noAllyp x;
no_getOf y;

to declare x as a variable of type struct Alias and y as a variable of type pointer to a struct Alias.

like image 39
axiac Avatar answered Sep 26 '22 08:09

axiac