C Unit-4
C Unit-4
Pointers:
Features of pointers
Declaration of Pointers-
arithmetic operations with pointers.
Structures:
Features of Structures –
Declaring and initialization of Structures –
Structure within Structure-
Array of Structures-
Enumerated data type-
Unions-
Features of Pointers:
1. Pointers save memory space.
2. Execution time with pointers is faster because data are manipulated with the address,
that is, direct access to memory location.
3. Memory is accessed efficiently with the pointers. The pointer assigns and releases the
memory as well. Hence it can be said the Memory of pointers is dynamically allocated.
4. Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional
arrays.
5. An array, of any type can be accessed with the help of pointers, without considering its
subscript range.
6. Pointers are used for file handling.
7. Pointers are used to allocate memory dynamically.
8. In C++, a pointer declared to a base class could access the object of a derived class.
However, a pointer to a derived class cannot access the object of a base class.
Advantages of Pointers
Pointers are used frequently in C, as they offer a number of benefits to the programmers.
They include the following:
#include<stdio.h>
void main()
{
int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization
}
Indirection (*) This operator is used to get the value from the pointed
address.
Reference operator (&) This operator is used to get the address of the variable
or pointer.
Assignment (=) You can assign the value to the pointer or value to the
address which is pointed by the pointer.
Addition (+) You can add integer value to the pointer to point the
different memory locations.
Subtraction (-) You can subtract the integer value from the pointer to
point the different memory locations.
comparison (==, !=, <, >, <=, This operation is valid only between two pointers that
and >=) point to the same array.
Incrementing (++) You can use increment operators (pre and post) with
the pointer.
Decrementing (–) You can use decrement operators (pre and post) with
the pointer.
➔ When we increment or decrement the pointer then pointer increase or decrease a block of
memory (block of memory depends on pointer data type).
Ex:
struct student
char name[20];
char rollno[15];
int age;
char grade;
};
We can also combine the structure definition and the declaration of structure variables into
a single line as shown below:
struct student
{
char name[20];
char rollno[15];
int age;
char grade;
} student1, student2, student3;
Structure Initialization
Like any other data type, a structure variable can be initialized at compile time.
An example of compile time initialization of the student structure is shown below:
struct student
{
char name[20];
char rollno[15];
int age;
char grade;
};
struct student student1 = {“Abhishek”, ”101”, 20, ’A’};
struct student student2 = {“Sri kanth”, ”102”, 21, ’B’};
struct student
{
struct
{
char fname[20];
char mname[20];
char lname[20];
}name;
char grade;
};
struct student s1;
strcpy(s1.name.fname, “Abhishek”);
In the above example, student is the outer structure and the inner structure name consists
of three members: fname, mname and lname.
Arrays of Structures
We use structures to describe the format of a number of related variables.
For example, we want to store details of 100 textbooks it will become difficult to declare
and maintain 100 variables and store the data.
Instead, we can declare and array of structure variables as shown below:
struct textbook
{
char name[40];
char author[20];
int pages;
}
struct textbook book[10];
In the above example, we are declaring an array book with 10 elements of the type textbook which
is a structure.
In the above syntax, the default value of int_const1 is 0, int_const2 is 1, int_const3 is 2, and
so on.
However, we can also change these default values while declaring the enum.
Below is an example of an enum named cars and how you can change the default values.
BMW=0, Ferrari=1, Jeep=2, and Mercedes-Benz=3. However, to change the default values,
we can define the enum as follows:
enum cars
{
BMW=3,
Ferrari=5,
Jeep=0,
Mercedes-Benz=1
};
Enumerated Type Declaration to Create a Variable
Similar to pre-defined data types like int and char, you can also declare a variable for enum
and other user-defined data types.
#include <stdio.h>
int main()
{
// printing the values of weekdays
for(int i=Sunday; i<=Saturday; i++)
{
printf("%d, ",i);
}
return 0;
}
Unions:
Unions have the same syntax as that of a structure since both of them are similar.
A structure is allocated memory for all the members of the structure whereas a union is
allocated memory only for largest member of the union.
This implies that, although a union may contain many members of different types, it can
handle only one member at a time.
Like structure, a union can be declared using the union keyword as shown below:
union student
{
char name[20];
char grade;
};
union student s1,s2;
So, the limitation on unions is: only one member can be used at a time.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
union student
{
char name[20];
char grade;
int marks;
};
union student s1;
strcpy(s1.name,"Abhishek");
s1.grade = 'A';
s1.marks = 98;
printf("Name is: %s \n",s1.name);
printf("Grade is: %c \n",s1.grade);
printf("Marks are: %d",s1.marks);
return 0;
}
Struct Union
1 The struct keyword is used to define a The union keyword is used to define union.
structure.
2 When the variables are declared in a structure, When the variable is declared in the union,
the compiler allocates memory to each the compiler allocates memory to the largest
variables member. The size of a structure is size variable member. The size of a union is
equal or greater to the sum of the sizes of each equal to the size of its largest data member
data member. size.
3 Each variable member occupied a unique Variables members share the memory space
memory space. of the largest size variable.
4 Changing the value of a member will not affect Changing the value of one member will also
other variables members. affect other variables members.
5 Each variable member will be assessed at a Only one variable member will be assessed
time. at a time.
6 We can initialize multiple variables of a In union, only the first data member can be
structure at a time. initialized.
7 All variable members store some value at any
Exactly only one data member stores a value
point in the program. at any particular instance in the program.
8 The structure allows initializing multiple Union allows initializing only one variable
variable members at once. member at once.
9 It is used to store different data type values.
It is used for storing one at a time from
different data type values.
10 It allows accessing and retrieving any data It allows accessing and retrieving any one
member at a time. data member at a time.