Académique Documents
Professionnel Documents
Culture Documents
What is Structure
Structure in c is a user-defined data type that enables us to store the collection of different
data types. Each element of a structure is called a member. Structures ca; simulate the use of
classes and templates as it can store various information
The ,struct keyword is used to define the structure. Let's see the syntax to define the structure
in c.
1. struct structure_name
2. {
3. data_type member1;
4. data_type member2;
5. .
6. .
7. data_type memeberN;
8. };
1. struct employee
2. { int id;
3. char name[20];
4. float salary;
5. };
The following image shows the memory allocation of the structure employee that is defined
in the above example.
Here, struct is the keyword; employee is the name of the structure; id, name, and salary are
the members or fields of the structure. Let's understand it by the diagram given below:
1st way:
Let's see the example to declare the structure variable by struct keyword. It should be
declared within the main function.
1. struct employee
2. { int id;
3. char name[50];
4. float salary;
5. };
The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and
e2 can be treated in the same way as the objects in C++ and Java.
2nd way:
Let's see another way to declare variable at the time of defining the structure.
1. struct employee
2. { int id;
3. char name[50];
4. float salary;
5. }e1,e2;
If number of variables are not fixed, use the 1st approach. It provides you the flexibility to
declare the structure variable many times.
If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in
main() function.
Let's see the code to access the id member of p1 variable by . (member) operator.
1. p1.id
C Structure example
Let's see a simple example of structure in C language.
1. #include<stdio.h>
2. #include <string.h>
3. struct employee
4. { int id;
5. char name[50];
6. }e1; //declaring e1 variable for structure
7. int main( )
8. {
9. //store first employee information
10. e1.id=101;
11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
12. //printing first employee information
13. printf( "employee 1 id : %d\n", e1.id);
14. printf( "employee 1 name : %s\n", e1.name);
15. return 0;
16. }
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
C Union
Like structure, Union in c language is a user-defined data type that is used to store the
different type of elements.
At once, only one member of the union can occupy the memory. In other words, we can say
that the size of the union in any instance is equal to the size of its largest element.
It occupies less memory because it occupies the size of the largest member only.
Disadvantage of union over structure
Only the last entered data can be stored in the union. It overwrites the data previously stored
in the union.
Defining union
The union keyword is used to define the union. Let's see the syntax to define union in c.
1. union union_name
2. {
3. data_type member1;
4. data_type member2;
5. .
6. .
7. data_type memeberN;
8. };
1. union employee
2. { int id;
3. char name[50];
4. float salary;
5. };
C Union example
1. #include <stdio.h>
2. #include <string.h>
3. union employee
4. { int id;
5. char name[50];
6. }e1; //declaring e1 variable for union
7. int main( )
8. {
9. //store first employee information
10. e1.id=101;
11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
12. //printing first employee information
13. printf( "employee 1 id : %d\n", e1.id);
14. printf( "employee 1 name : %s\n", e1.name);
15. return 0;
16. }
Output:
employee 1 id : 1869508435
employee 1 name : Sonoo Jaiswal
Let’s understand the difference between structure and union, along with a comparison chart.
Structure Vs Union
Comparison Chart
Basis of
Structure Union
Comparison
Stores distinct values for all the Stores same value for all
Store Value
members. the members.
Output:
//Pointer declaration
int *p;
Output:
We have already seen in the first example that we can display the address of a variable using
ampersand sign. I have used &num to access the address of variable num. The & operator is
also known as “Address of” Operator.
Point to note: %p is a format specifier which is used for displaying the address in hex
format.
Now that you know how to get the address of a variable but how to store that address in
some other variable? That’s where pointers comes into picture. As mentioned in the
beginning of this guide, pointers in C programming are used for holding the address of
another variables.
Pointer is just like another variable, the main difference is that it stores address of
another variable rather than a value.
Output:
A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name. For example, the following declaration declares a
pointer to a pointer of type int −
int **var;
When a target value is indirectly pointed to by a pointer to a pointer, accessing that value
requires that the asterisk operator be applied twice, as is shown below in the example −
Live Demo
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
return 0;
}
When the above code is compiled and executed, it produces the following result −
As it can be seen that the length (size) of the array above made is 9. But what if there is a
requirement to change this length (size). For Example,
If there is a situation where only 5 elements are needed to be entered in this array. In this
case, the remaining 4 indices are just wasting memory in this array. So there is a
requirement to lessen the length (size) of the array from 9 to 5.
Take another situation. In this, there is an array of 9 elements with all 9 indices filled. But
there is a need to enter 3 more elements in this array. In this case 3 indices more are
required. So the length (size) of the array needs to be changed from 9 to 12.
Therefore, C Dynamic Memory Allocation can be defined as a procedure in which the size
of a data structure (like Array) is changed during the runtime.
C provides some functions to achieve these tasks. There are 4 library functions provided by C
defined under <stdlib.h> header file to facilitate dynamic memory allocation in C
programming. They are:
1. malloc()
2. calloc()
3. free()
4. realloc()
1. C malloc() method
“malloc” or “memory allocation” method in C is used to dynamically allocate a
single large block of memory with the specified size. It returns a pointer of type void
which can be cast into a pointer of any form.
Syntax:
For Example:
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And,
the pointer ptr holds the address of the first byte in the allocated memory.
Example:
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
#include <stdlib.h>
int main()
int* ptr;
int n, i, sum = 0;
n = 5;
if (ptr == NULL) {
exit(0);
else {
ptr[i] = i + 1;
return 0;
Output:
2. C calloc() method
Syntax:
For Example:
This statement allocates contiguous space in memory for 25 elements each with the
size of the float.
If space is insufficient, allocation fails and returns a NULL pointer.
Example:
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
#include <stdlib.h>
int main()
int* ptr;
int n, i, sum = 0;
n = 5;
if (ptr == NULL) {
exit(0);
}
else {
ptr[i] = i + 1;
return 0;
Output:
3. C free() method
Syntax:
free(ptr);
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
int n, i, sum = 0;
n = 5;
exit(0);
else {
free(ptr);
free(ptr1);
return 0;
Output:
Syntax:
#include <stdio.h>
#include <stdlib.h>
int main()
int* ptr;
int n, i, sum = 0;
// Get the number of elements for the array
n = 5;
if (ptr == NULL) {
exit(0);
else {
ptr[i] = i + 1;
n = 10;
ptr[i] = i + 1;
free(ptr);
return 0;
Output:
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%p\n", ptr);
return 0;
}
In this program, we have a pointer ptr that points to the 0th element of the array. Similarly, we
can also declare a pointer that can point to whole array instead of only one element of the
array. This pointer is useful when talking about multidimensional arrays.
Syntax:
data_type (*var_name)[size_of_array];
Example:
int (*ptr)[10];
Here ptr is pointer that can point to an array of 10 integers. Since subscript have higher
precedence than indirection, it is necessary to enclose the indirection operator and pointer
name inside parentheses. Here the type of ptr is ‘pointer to an array of 10 integers’.
Note : The pointer that points to the 0th element of array and the pointer that points to the
whole array are totally different. The following program shows this:
Function Pointer in C
In C, like normal data pointers (int *, char *, etc), we can have pointers to functions.
Following is a simple example that shows declaration and function call using function
pointer.
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
void fun(int a)
int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int);
fun_ptr = &fun;
*/
(*fun_ptr)(10);
return 0;
Output:
Value of a is 10
int a = 10;
char b = 'x';
int main(void)
{
// Note that malloc() returns void * which can be
Note that the above program compiles in C, but doesn’t compile in C++. In C++, we must
explicitly typecast return value of malloc to (int *).
2) void pointers in C are used to implement generic functions in C. For example compare
function which is used in qsort().
#include<stdio.h>
int main()
int a = 10;
printf("%d", *ptr);
return 0;
Output:
#include<stdio.h>
int main()
int a = 10;
Output:
10
2) The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it
is allowed by considering the size of void is 1. For example the following program compiles
and runs fine in gcc.
#include<stdio.h>
int main()
return 0;
Output: