Structs are the user defined group of similar or different data types. Similar or different group of data types can be grouped under one name known as structures. Example declaration of structure in C:
Int a;
Char b;
Float c;
};
To create object from the structure declaration, here is the syntax in C to do so:
With this object creation, memory is assigned to the structure. To access the member of structures with the help of object(obj) can be done as:
This is the case when object is created, there is no need to allocate any memory. Once the object is instantiated memory will be allocated automatically.
Allocation of memory to the structure can be done at runtime. For runtime allocation, malloc function can be used.
There will be a declaration of pointer to the structure. Declaration of the pointer to the structure can be done as struct abc *ptr; With this declaration, there will not be assignment of any memory to the structure object. Malloc function should be used to allocate memory and below is the syntax to do so:
Now, ptr will have the memory assigned. It can store the data for member elements of structure.
To access the structure members with the help of pointers can be done as follows:
So far, we discussed the single structure object. Both the ways of access of member elements, we have seen through pointers and the direct access with objects.
We have two ways to define the structures, first is to define the structure object and another way is to define the pointer to the structure.
Structure pointer: Struct abc *ptr;
Now, let us discuss the array of structure objects and array of structure pointers. Array is a group of objects of same type. For example array of obj will be declared as struct abc obj[128]. Array of pointers to the structure objects will be as struct abc *ptr[128]. Both the array defined the 128 elements of structure objects and pointers.
C program to declare structure object:
struct abc{
int a;
char b;
float c;
};
int main()
{
struct abc obj = {1,'c',3.4};
printf("a=%d,b=%c,c=%f\n",obj.a,obj.b,obj.c);
return 0;
}
Output:
a=1,b=c,c=3.400000
bash-4.2$
Above program defines the object of structure and a way to initialize the member while declaring the object. We initialize the member variables with specific values and print those variables by accessing the members with object directly. a is assigned with 1, b is assigned with ‘c’ and c is assigned with float value 3.4. Below is the snapshot of the program and output.
Snapshot:
C program to declare the pointer to the structure:
Output:
a=4,b=d,c=5.500000
bash-4.2$
Above program defines the pointer to object of structure. Malloc function is used to allocate memory for the pointer variable. We initialize the member variables with specific values and print those variables by accessing the members with pointer. a is assigned with 4, b is assigned with ‘d’ and c is assigned with float value 5.5. Below is the snapshot of the program and output.
Snapshot:
Now, let us go through the C program for array of structures and array of pointers to structures.
C program for array of structure of objects:
struct abc{
int a;
char b;
float c;
};
int main()
{
struct abcobj[2];
obj[0].a = 4;
obj[0].b = 'd';
obj[0].c = 5.5;
obj[1].a = 5;
obj[1].b = 'f';
obj[1].c = 8.2;
printf("[0]a=%d,[0]b=%c,[0]c=%f\n",obj[0].a,obj[0].b,obj[0].c);
printf("[1]a=%d,[1]b=%c,[1]c=%f\n",obj[1].a,obj[1].b,obj[1].c);
return 0;
}
Output:
[0]a=4,[0]b=d,[0]c=5.500000
[1]a=5,[1]b=f,[1]c=8.200000
bash-4.2$
Above program defines the array of object of structure and a way to initialize the members with the help of objects. We initialize the member variables with specific values and print those variables by accessing the members with object directly. For simplicity, we have taken only 2 objects. First object’s a is assigned with 4, b is assigned with ‘d’ and c is assigned with float value 5.5. second object’s a is assigned with 5, b is assigned with ‘f’ and c is assigned with float value 8.2. Below is the snapshot of the program and output.
Snapshot:
C program for array of pointers to structure of objects:
struct abc{
int a;
char b;
float c;
};
int main()
{
struct abc *ptr[2];
ptr[0] = malloc(sizeof(struct abc));
ptr[1] = malloc(sizeof(struct abc));
ptr[0]->a = 4;
ptr[0]->b = 'd';
ptr[0]->c = 5.5;
ptr[1]->a = 5;
ptr[1]->b = 'f';
ptr[1]->c = 8.2;
printf("[0]a=%d,[0]b=%c,[0]c=%f\n",ptr[0]->a,ptr[0]->b,ptr[0]->c);
printf("[1]a=%d,[1]b=%c,[1]c=%f\n",ptr[1]->a,ptr[1]->b,ptr[1]->c);
return 0;
}
Output:
[0]a=4,[0]b=d,[0]c=5.500000
[1]a=5,[1]b=f,[1]c=8.200000
bash-4.2$
Above program defines the array of pointers to object of structure and a way to initialize the members with the help of pointers. We initialize the member variables with specific values and print those variables by accessing the members with pointer variables. For simplicity, we have taken only 2 pointers. First pointer to object a is assigned with 4, b is assigned with ‘d’ and c is assigned with float value 5.5. second pointer to object a is assigned with 5, b is assigned with ‘f’ and c is assigned with float value 8.2. Below is the snapshot of the program and output.
Snapshots:
Conclusion:
We discussed the structure data type in C and the ways to declare the objects and pointers to structure objects. We also discussed few examples and with output. Both the objects and pointers were discussed. Array of objects and pointer to objects also were discussed with examples.