Unit4 Material
Unit4 Material
Structure is a collection of variables of different data types. Structure name or structure tag
should always precede with the keyword "struct". Structure declaration always enclosed
between 2 braces{}(curly braces). The data types declared inside a structure are known as
members of structure.
Syntax:
struct struct_name
{
data type membername1;
data type membername2;
}variable1, variable2;
Example:
struct student
{
char stud_name[30];
int roll_number;
float percentage;
};
The memory is allocated when we create the variable of a particular structure. The size of
memory allocated is equal to the sum of memory required by individual members of that
structure.
Initialization of Structures:
A structure can be initialized by assigning values to the members within structure.
Struct structure_name var = {value1, value2...};
C Program to demonstrate Structures
#include<stdio.h>
struct item
{
int x;
int y;
};
int main( )
{
struct item it;
it.y = 20;
it.x = 12;
printf("size of item is %d \n",sizeof(it));
printf("%d\n", it.x);
printf("%d\n", it.y);
return 0;
}
Output:
size of item is 8
12
20
Unions:
----------
C Union is also like structure, i.e. collection of different data types which are grouped
together. Each element in a union is called member. Union and structure in C are same in
concepts, except allocating memory for their members. Structure allocates storage space for
all its members separately, whereas, union allocates one common storage space for all its
members.
We can access only one member of union at a time. We can’t access all member values at the
same time in union. But, structure can access all member values at the same time.
Synatx:
union name
{
data type membername1;
data type membername2;
};
Example:
unioin student
{
char stud_name[30];
int roll_number;
float percentage;
};
The size of memory allocated is equal to the maximum memory required by an individual
member among all members of that union.
Features of pointers:
• Pointers save the memory space.
• Execution time with pointer is faster because data is manipulated with the address, i.e.,
direct access to memory location.
• Pointers are used to allocate memory dynamically.
• The memory is accessed efficiently with the pointers. The pointer assigns the memory space
and also releases it.
• Pointers are used for file handling.
Pointer Declaration:
Pointer variables are declared as follows:
Syntax:
datatype* identifier;
Example: int* a;
float* f;
char* y;
In the first statement ‘x’ is an integer pointer and it tells the compiler that it holds the address
of any integer variable. In the same way, ‘f’ is a float pointer which holds the address of any
float variable and ‘y’ is character pointer which holds the address of any character variable.
• The indirection operator (*) is also called the dereferencing operator. When a pointer is
dereferenced, the value at that address stored by the pointer is retrieved.
• The indirection operator (*) is used in 2 different ways with pointers, declaration and
dereference.
• When a pointer is declared, an asterisk (*) indicates that it is a pointer and not a normal
variable.
• When a pointer is dereferenced, the indirection operator (*) indicates that the value at that
memory location stored in the pointer is to accessed rather than the address itself.
#include<stdio.h>
int main()
{
int a;
int* pa;
a = 10;
pa = &a;
printf(“\n %u”, pa);
printf(“\n %u”, &a);
printf(“\n %d”, a);
printf(“\n %d”, *pa);
printf(“\n %d”, *(&a));
printf(“\n %u”, &pa);
}
Output:
2000
2000
10
10
10
3000
#include <stdio.h>
float b = 22.22;
float *q = &b;
printf("q = %u\n", q); //q = 6422284
q++;
printf("q++ = %u\n", q); //q++ = 6422288 +4 // 4 bytes
q--;
printf("q-- = %u\n", q); //q-- = 6422284 -4 // restored to original value
return 0;
} Output
p = 1441900792
p++ = 1441900796
p-- = 1441900792
q = 1441900796
q++ = 1441900800
q-- = 1441900796
r = 1441900791
r++ = 1441900792
r-- = 1441900791
6. Array of Pointers:
Array of pointers is nothing but a collection of addresses. Here, we store the addresses of
variables for which we have to declare the array as pointer. This structure is especially
helpful when the number of elements in an array is variable.
Example:
#include<stdio.h>
int main()
{
int a[5] = {0,1,2,3,4}, i;
int* p[5];
for(i = 0; i<5; i++)
p[i] = a + i;
for(i = 0; i<5; i++)
{
printf(“\n\t%d at location”, *(*p+i));
printf(“\n\t%u at location”, *(p+i));
printf(“%u at location”, p+i);
}
}
Output:
0 at location 4036 at location 4046
1 at location 4038 at location 4048
2 at location 4040 at location 4050
3 at location 4042 at location 4052
4 at location 4044 at location 4054
Explanation:
In the above program, the first for loop assigns the addresses of the elements o integer array
to pointer the array. The first printf() statement prints elements, the second displays addresses
of the element and the third displays addresses of the address i.e., address of the pointer.