Unit 4 Introduction To Programming-1
Unit 4 Introduction To Programming-1
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.
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
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.
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(.)