w3resource

C Language: Difference between typedef struct and struct definitions with example

C - Difference between typedef struct and struct

In C language, struct is used to define a user-defined data type that groups together variables of different data types under a single name.
A typedef can be used to create an alias for an existing data type, including a struct.

Here is an example of using a struct definition to create a custom data type:

Code:

struct student {
   char name[50];
   int age;
   float height;
};
struct student s1 = {" Nina Chase", 12, 1.55};
struct student s2 = {" Shyann Morris", 12, 1.65};

In the above example, we define a struct called student that contains three member variables: name, age, and height. We then create two instances of the student struct called s1 and s2 and initialize its member variables.

Now let's see an example of using a typedef with a struct:

Code:

typedef struct {
   char name[50];
   int age;
   float height;
} student;

student s1 = {" Nina Chase", 12, 1.55};
student s2 = {" Shyann Morris", 12, 1.65};

In the above example, we define a typedef that creates an alias for a struct containing the same three member variables as before. We then create two instances of the student struct using the typedef aliases s1 and s2 and initialize its member variables.

The main difference between using typedef and struct directly is that typedef allows us to create an alias for the struct. This alias is then used later to define variables of that type without having to write the struct keyword.

Contribute your code and comments through Disqus.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - How do you pass a function as a parameter in C?

Declaration

A prototype for a function which takes a function parameter looks like the following:

void func ( void (*f)(int) );

This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:

void print ( int x ) {
  printf("%d\n", x);
}

Function Call

When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:

func(print);

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:

for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
  print(ctr);
}

Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:

void func ( void (*f)(int) ) {
  for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
    (*f)(ctr);
  }
}

Ref : https://bit.ly/3skw9Um