Prog. With C & C++ (Unit-IV)
Prog. With C & C++ (Unit-IV)
1. Pointers
Introduction to Pointers:
Pointer is also a variable which can hold the address of another variable (address
of a normal variable).
While developing any program in C, whenever we declare a variable then the
sufficient memory will be allocated for that variable and also by default the syatem will
provide a specific address for that memory location.
Example: 2 bytes size of variable a
int a; a 10 value of a
a=10; 1002 address of a
Note: In C programming language we can access the values which are stored in the
memory locations in two ways. They are;
1. By using variable name (Direct access)
2. By using address of variable (Indirect access)
In indirect access we use the concept called ponters.
In the above example, variable n cannot store the address of a. Because n is a normal
variable. To store the address of any variable we require a pointer variable.
Declaration of Pointers:
Declaration of pointers is nothing but defining its name and type to allocate the
memory space for the pointers.
Or
Initialization of Pointers:
Note 1: A normal variable must be declared before the declaration of pointer variable.
Example:
int *p;
p=&a;
int a;
a=10;
The above statements are not valid statement. Because a normal variable a is
declared after pointer variable declaration.
Note 2: Normal variable type and pointer variable type must be same.
Example:
float a;
a=10.50;
int *p;
p=&a;
The above statements are not valid. Because normal variable type is float and
pointer variable type is int.
Indirection operator ( * ):
In C programming language we can develop any program in two ways. They are;
1. Without pointers (Direct access)
2. By using pointers (Indirect access)
Indirection operator is used to access the values through the pointers. This
indirection operator should be written before the pointer variable.
Example:
int a;
a=10; a 10
int *p; 1002
p=&a; p 1002
` #include<stdio.h>
void main()
{
int a, b, c;
a=10;
b=20;
int *p1, *p2;
p1=&a;
p2=&b;
c=*p1 + *p2; // pointer expression
printf(“The addition value = %d”, c);
}
Output:
The addition value = 30
Ex-2: Write a program to perform subtraction operation using indirection operator (pointer
expression).
#include<stdio.h>
void main()
{
int a, b, c;
a=10;
b=20;
int *p1, *p2;
p1=&a;
p2=&b;
c=*p1 - *p2; // pointer expression
printf(“The subtraction value = %d”, c);
}
Output:
The subtraction value = -10
Features of Pointers:
Pointers have many features. Some they are discussed below;
2. Structures
Features of Structures:
Structure is an example for user-defined data type where we can store multiple
values of different types.
Here is an example of how we can simply equate two structures to copy the
values from one to another.
2) Nested Structures:
This feature of structure allows us to pass any of the structure variables to the
function like any other normal variable. This can be done in two ways: Passing
individual structure elements to the function or passing the entire structure to the
function.
Declaring a structure:
Syntax:
struct <Structurename>
{
member1;
member2;
member3;
.
.
.
membern;
};
Whenever the statements are executed then internally memory will be allocated
for all the structure members with respective structure variables as below;
accno
name
balance
Initializing a structure:
As we know the value of a normal variable can be accessed directly by its name.
To access the elements of an array we use its indexes. In the same way to access the
members of structures we can use member operator (dot operator).It is denoted by the
symbol dot ( . ).
#include<stdio.h>
#include<conio.h>
void main()
{
struct account
{
int accno;
char name[];
float balance;
};
struct account person1, person2 = , ,1001, “Jyothi”, 25000.00-, ,1002, “Krish”, 28000.00- -;
printf(“ Person-1 Details: ”);
printf(“ \n Account Number = %d “, person1.accno);
printf(“\n Name = %s “, person1.name);
printf(“ \n Balance = %f “, person1.balance);
printf(“ \n Person-2 Details: ”);
printf(“ \n Account Number = %d “, person2.accno);
printf(“ \n Name = %s “, person2.name);
printf(“ \n Balance = %f “, person2.balance);
}
Output:
Person-1 Details:
Account Number = 1001
Name = Jyothi
Balance = 25000.00
Person-2 Details
Account Number = 1002
Name = Krish
Balance = 28000.00
Arrays of structures:
If there are 50 persons (variables) then no need to declare all the 50 variable
names. Simply we can declare one array with size 50 as below;
After declaring an array variable, we can store the values into the array as below;
Whenever the above statement is executed, all the values will be stored into the
memory locations as beloq;
To access the values of the array of structure we can use their indexes along with
member name as below;
#include<stdio.h>
#include<conio.h>
void main()
{
int I;
clrscr();
struct account
{
int accno;
char namr[];
float balance;
};
struct account person[5];
for(i=0;i<5;i++)
{
printf(“Enter Person %d Details: “, i+1);
scanf(“%d%s%f”, &person*i+.accno, person*i+.name, &person*i+.balance);
}
for(i=0;i<5;i++)
{
printf(“Person %d Details: “, i+1);
printf(“\n Account Number: “, person*i+.accno);
printf(“\n Name: “, person*i+.name);
printf(“\n Balance: “, person*i+.balance);
}
}
Enumeration is also an example for user defined data type same as structures and
unions. Simply these are also called enums.
Note: Programmers can also assign the integers values to the members.
#include<stdio.h>
void main()
{
enum month
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
enum month a, b;
a=march;
b=August;
printf(“The value of March = %d”, a);
printf(“The value of August =%d”, b);
}
3. Unions:
Introduction to Unions:
Union is also an example for user-defined data type where we can store multiple
values of different types.
Declaring a union:
Syntax:
union <Unionname>
{
member1;
member2;
member3;
.
.
.
membern;
};
Declaring a union variable is nothing but allocating sufficient memory for union
members.
Whenever the statements are executed then internally memory will be allocated
for all the union members with respective union variable as below;
person
accno
name
balance
Note-1: If we declare a structure variable then the memory will be created for each and
every structure member separately. Whereas, if we declare the variable for union then
only one memory location is allocated for all the union members.
Note-2: Union members are also accessed by using dot operator same as structure
members.
#include<stdio.h>
void main()
{
union account
{
int accno;
char name[];
float balance;
};
union account person;
printf(“Enter Person Details: “);
scanf(“%d%s%f”, &person.accno, person.name, &person.balance);
printf(“ \n Person Details: ”);
printf(“ \n Account Number = %d “, person.accno);
printf(“\n Name = %s “, person.name);
printf(“ \n Balance = %f “, person.balance);
}
Structures Unions
Structure is an example for User-defined Union is also an example for User-
data type. defined data type.
The keyword struct is used to declare The keyword union is used to declare the
the structures. unions.
Syntax: Syntax:
struct <structurename> union <unionname>
{ {
Member1; Member1;
Member2; Member2;
….. …..
}; };