0% found this document useful (0 votes)
61 views

Unit 4 Introduction To Programming-1

Uploaded by

shabanasarwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Unit 4 Introduction To Programming-1

Uploaded by

shabanasarwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT IV

Pointers, dereferencing and address operators, pointer and address arithmetic, array manipulation using pointers,
User-defined data types-Structures and Unions.

Pointer
A pointer is a variable that holds a memory address. This address is the location of another object (typically
another variable, array, structure, function, file, etc) in memory.
For example, if one variable contains the address of another variable, the first variable is said to pointer.
Figure illustrates this situation.

Fig.One variable point to another

Pointer Variable Declaration


A pointer declaration consists of a data type, an *, and the variable name.
Syntax:
type *name;

Pointer Variable Initialization


Pointer Initialization is the process of assigning address of a variable to pointer variable.
In C language address operator & is used to determine the address of a variable.
Example:
int a = 10 ; //variable declaration and initialization
int *ptr ; //pointer declaration
ptr = &a ; //pointer initialization
or,
int *ptr = &a ; //pointer declaration and initialization

Pointer variable always points to same type of data.

float a;
int *ptr;
ptr = &a; //ERROR, type mismatch
The Pointer Operators
There are two pointer operators:
Dereferencing operator (*):
The dereferencing operator (*), is a unary operator that returns value (data) at address.
Address operator (&):
Address operator (&) is a unary operator that returns the memory address of its operand.
//example program on pointer operators
#include<stdio.h>
#include<conio.h>
void main()
{
int i=3;//variable declaration and initialization
int *p; //pointer declaration
clrscr();
p=&i;//pointer initialization
printf("\nThe value of i is:%d",i);
printf("\nThe value of i is:%d",*p); p i
printf("\nThe value of i is:%d",*(&i)); 65524 3
printf("\nThe Adress of i is:%u",&i);
printf("\nThe Address of p is:%u",&p); 65522 65524
getch();

}
Output:
The value of i is:3
The value of i is:3
The value of i is:3
The Address of i is:65524
The Address of p is:65522

Pointer and address arithmetic:


There are only two arithmetic operations that you can use on pointers:
Addition.
The Rule to increment the pointer is given below:
new_address= current_address + i * size_of(data type)
Where i is the number by which the pointer get increased.
For 16 bit int variable, integer pointer will be incremented by 2 bytes.
For 32 bit int variable, integer pointer will be incremented by 4 bytes.
 Character pointer will be incremented by 1 byte.
 Float pointer will be incremented by 4 bytes.
 Double pointer will be incremented by 8 bytes.
//program on pointer addition
#include<stdio.h>
#include<conio.h>
void main()
{
char c='a',*cp=&c;
int i=10,*ip=&i;
float f=3.14,*fp=&f;
double d=5.14,*dp=&d;
clrscr();
printf("The address in character pointer is:%u",cp);
printf("\nThe character pointer after increment is:%u",++cp); //cp=cp+1*1
printf("\nThe address in integer pointer is:%u",ip);
printf("\nThe integer pointer after increment is:%u",++ip); //ip=ip+1*2
printf("\nThe address in float pointer is:%u",fp);
printf("\nThe float pointer after increment is:%u",++fp); //fp=fp+1*4
printf("\nThe address in double pointer is:%u",dp);
printf("\nThe double pointer after increment is:%u",++dp); //dp=dp+1*8
getch();
}
Output:

Subtraction.
The Rule to decrement the pointer is given below:
new_address= current_address - i * size_of(data type)
Where i is the number by which the pointer get decreased.
16 bit int variable, integer pointer will be decremented by 2 bytes.
32 bit int variable, integer pointer will be decremented by 4 bytes.
Character pointer will be decremented by 1 byte.
Float pointer will be decremented by 4 bytes.
Double pointer will be decremented by 8 bytes.
// program on pointer subtraction.
#include<stdio.h>
#include<conio.h>
void main()
{

char c='a',*cp=&c;
int i=10,*ip=&i;
float f=3.14,*fp=&f;
double d=5.14,*dp=&d;
clrscr();
printf("The address in character pointer is:%u",cp);
printf("\nThe character pointer after decrement is:%u",--cp); //cp=cp-1*1
printf("\nThe address in integer pointer is:%u",ip);
printf("\nThe integer pointer after decrement is:%u",--ip); //ip=ip-1*2
printf("\nThe address in float pointer is:%u",fp);
printf("\nThe float pointer after decrement is:%u",--fp); //fp=fp-1*4
printf("\nThe address in double pointer is:%u",dp);
printf("\nThe double pointer after decrement is:%u",--dp); //dp=dp-1*8
getch();
}
Output:

Besides addition and subtraction of a pointer and an integer, only one other arithmetic operation is allowed:
You can subtract one pointer from another.
All other arithmetic operations are prohibited, such as
 We cannot multiply or divide pointers.
 We cannot add two pointers.
 We cannot apply the bitwise operators to them.

Array manipulation using pointers.


When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all
the elements of the array in contiguous memory locations. The base address is the location of the first element
(index 0) of the array.
Example:
int x[5] = {1, 2, 3, 4, 5};
Suppose the base address of x is 1000 and assuming that each integer requires two bytes, the five elements will
be stored as follows:

The name x is defined as a constant pointer pointing to the first element, x[0] and therefore the value of x is
1000, the location where x[0] is stored. That is,
x = &x[0] = 1000
If we declare p as an integer pointer, then we can make the pointer p to point to the array x by the following
assignment:

p = x;
This is equivalent to
p = &x[0];
Now, we can access every value of x using p++ to move from one element to another.
The relationship between p and x is shown as:
p = &x[0] (= 1000)
p+1 = &x[1] (= 1002)
p+2 = &x[2] (= 1004)
p+3 = &x[3] (= 1006)
p+4 = &x[4] (= 1008)
You may notice that the address of an element is calculated using its index and the scale factor of the data type.
For instance,
address of x[3] = base address + (3 x scale factor of int) = 1000 + (3 x 2) = 1006
When handling arrays, instead of using array indexing, we can use pointers to access array elements.
Note that *(p+3) gives the value of x[3]. The pointer accessing method is much faster than array indexing.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p, sum=0, i;
int x[5] = {5,9,6,3,7};
clrscr();
i = 0;
p = x; /* initializing with base address of x */
printf("Element Value Address\n\n");
while(i < 5)
{
printf(" x[%d] %d\t%u\n", i, *p, p);
sum = sum + *p; /* accessing array element */
i++;
p++; /* incrementing pointer */
} Index  0 1 2 3 4
printf("\n Sum = %d", sum); Elements  5 9 6 3 7
printf("\n &x[0] = %u", &x[0]); Addresses  65514 65516 65518 65520 65522
printf("\n p = %u", p); Array Name: x
getch();
}
Output:
User-defined data types:
Structures:
The structure is a user-defined data type that can be used to group different types of items.
Differences between array and structure
S.no Array Structure
1 Arrays can only hold elements of the same Structures can hold elements of different data
data type(homogeneous). types(heterogeneous)
2 Array data can be accessed using index. Structures data can be accessed using dot(.) , arrow(->)
operators.
3 Arrays store data in contiguous memory Structures do not require the data to be stored in
locations. consecutive memory locations.
4 Array is a derived data type. Structure is a user defined data type.
5 Accessing array elements are fast Accessing structure elements are slow.
6 Elements in an Array are always of the same size Elements in a Structure can be of different sizes.
Declaration:
Syntax:
struct <tagname>
{
data_type member_name1;
data_type member_name2;
};
Example:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
The keyword struct declares a structure to hold the details of four data fields, namely title, author, pages, and
price. These fields are called structure elements or members. Each member may belong to a different type of
data. book_bank is the name of the structure and is called the structure tag.
Declaring Structure Variables:
After declaring a structure format we can declare variables of that type. A structure variable declaration is
similar to the declaration of variables of any other data types. It includes the following elements:
1. The keyword struct.
2. The structure tag name.
3. List of variable names separated by commas.
4. A terminating semicolon.
Example:
struct book_bank book1;
Initializing structure members:
1.Using = and {}
Syntax:
struct stutcture_tag structure_variable_name={value1,value2,value3,…,valueN};
Example:
struct book_bank book1={“c programming”,”Dennis Ritche”,555,250.50};
2. Using scanf() function.
Example:
scanf(“%d”,&book1.pages);
Accessing members of the Structure:
The members of the structure can be accessed with a variable declared of that structure type and with dot
operator(.)

The general form is shown below:


Structure variable name.member name
Example:
book1.pages
// Example program on structure book1
#include<stdio.h>
#include<conio.h>
void main()
{
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
clrscr();
struct book_bank book1={"c programming","Dennis Ritche ",589,250.50};
printf("The book name is:%s",book1.title);
printf("\nThe book author is:%s",book1.author);
printf("\nThe book price is:%.2f",book1.price);
printf("\nThe book pages are:%d",book1.pages);
getch();
}
Output:
//Another example program on structure
#include<stdio.h>
#include<conio.h>
void main()
{
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
}book1;
struct book_bank *ptr;
ptr=&book1;
clrscr();
printf("Enter book title:");
gets(book1.title);
printf("Enter author name:");
gets(book1.author);
printf("Enter book price:");
scanf("%f",&book1.price);
printf("Enter book pages:");
scanf("%d",&book1.pages);
printf("\nThe book name is:%s",ptr->title);
printf("\nThe book author is:%s",ptr->author);
printf("\nThe book price is:%.2f",ptr->price);
printf("\nThe book pages are:%d",ptr->pages);
getch();
}
Output:
Unions:
The Union is a user-defined data type in C language that can contain elements of the different data types just
like structure. But unlike structures, all the members in the C union are stored in the same memory location.
Due to this, only one member can store data at the given instance.
s.no Structure Union
1 The keyword struct is used to define structure. The keyword union is used to define union.
2 Memory is allocated for each member. Memory is allocated for largest member only.
3 Several members of the structure can be Only one member of the union can be initialized at a
initialized at once. time.
4 Consume more space compare to union. Compare less space compare to structure.
5 Individual members can be accessed at a time. Only one member can be accessed at a time.

// example program on union


#include<stdio.h>
#include<conio.h>
void main()
{
union book
{
char book[15];
int pages;
float price;
};
union book b;
clrscr();
printf("Enter Book name:");
gets(b.book);
printf("The Book name is:%s",b.book);
printf("\nEnter Book pages:");
scanf("%d",&b.pages);
printf("The book pages are:%d",b.pages);
printf("\nEnter book price:");
scanf("%f",&b.price);
printf("The book price is:%.2f",b.price);
getch();
}
Output:

You might also like