Structures in C

Structures in C: The Structures are nothing but user-defined data types that allows combining data items of different kinds. And these structures are used to represent a record. So, for defining a structure we should use struct statement.

Structures in C with Syntax & Examples

To define structures, you should use a struct statement. And the structure tag is optional and each member definition is a normal variable definition, such as int I or floats f; or any other valid variable definition.

Syntax
struct
{
member definition;
member definition;

member definition;
} [one or more structure variables];



Example: a program for defining a book in structures

struct employee  
{   int id;  
    char name[20];  
    float salary;  
};

structure in C

Accessing Structure Members in C

For accessing any member of the C structure, we should use the member access operator(.). While the member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.

Syntax
var_name.member1_name;
var_name.member2_name;

Example

 #include <stdio.h>             
#include <string.h>            
struct student {                  
int id;                  
char name[20];                  
float percentage; } 
record;       
int main() {            
record.id=1;            
strcpy(record.name, "Raju");            
record.percentage = 86.5;            
printf(" Id is: %d \n", record.id);            
printf(" Name is: %s \n", record.name);            
printf(" Percentage is: %f \n", record.percentage);            
return 0;    }

Output
Id is: 1
Name is: Raju
The percentage is: 86.500000

Nested structures in C

Using a structure inside another structure is known as “Nested structure”.

Example

struct stu_address{int street;
char *state;
char *city;
char *country; }
struct stu_data{int stu_id;int stu_age;char *stu_name;struct stu_address stuAddress; }
struct stu_data mydata;
mydata.stu_id = 1001;
mydata.stu_age = 30;
mydata.stuAddress.state = "UP";//Nested struct assignment..

Using typedef in structure

In C typedef makes the code short and improves the readability. We can also that typedef is an alias of a struct.

Example: Code without a typedef

struct home_address {
int local_street;
char *town;
char *my_city;
char *my_country;};
...struct home_address var;
var.town = "Agra";

Example 2: Code with a typedef

typedef struct home_address{
int local_street;
char *town;
char *my_city;
char *my_country;}
addr;
....addr var1;
var.town = "Agra"

Instead of using the struct home_address every time you need to declare struct variable, you can simply use addr, the typedef that we have defined.

Structures to Pointers

Structures can be accessed using pointers.

Output
struct name {
member1;
member2;


};
int main()
{
struct name *ptr, Harry;
}

Example

#include<stdio.h>
struct person
{
   int age;
   float weight;
};
int main()
{
    struct person *personPtr, person1;
    personPtr = &person1;   

    printf("Enter age:");
    scanf("%d", &personPtr->age);

    printf("Enter weight:");
    scanf("%f", &personPtr->weight);

    printf("Displaying:\n");
    printf("Age: %d\n", personPtr->age);
    printf("weight: %f", personPtr->weight);

    return 0;
}

Output
Enter age:24
Enter weight:48
Displaying:
Age: 24
weight: 48.000000

Dynamic memory allocation of structures

Example

#include<stdio.h>
struct person
{
   int age;
   float weight;
};
int main()
{
    struct person *personPtr, person1;
    personPtr = &person1;   
    printf("Enter age:");
    scanf("%d", &personPtr->age);
    printf("Enter weight:");
    scanf("%f", &personPtr->weight);
    printf("Displaying:\n");
    printf("Age: %d\n", personPtr->age);
    printf("weight: %f", personPtr->weight);
    return 0;
}

Output
Enter age:24
Enter weight:48
Displaying:
Age: 24
weight: 48.000000