lab #9
lab #9
Objectives
Whenever a variable is declared, system will allocate a location to that variable in the memory, to hold
value. This location will have its own address number.
Let us assume that system has allocated memory location 80F (Hexa decimal representation of memory
address) for a variable a.
int a = 10 ;
We can access the value 10 by either using the variable name a or the address 80F. Since the memory
addresses are simply numbers they can be assigned to some other variable. The variable that holds
memory address are called pointer variables. A pointer variable is therefore nothing but a variable that
contains an address, which is a location of another variable. Value of pointer variable will be stored in
another memory location.
Declaring and initializing a pointer variable:
datatype * identifier ;
int *a;
The meaning of the above statement is that a is a pointer variable that holds address in which integer
data is stored.
Note: a itself is not an integer
Example 1: Write a program to declare pointer variables that point to float data, character data.
Initialize them with appropriate variables. Print the addresses stored and size of memory used by these
pointer variables.
Unlike the variable the pointer variables also will have junk/garbage values. They have to be initialized
before using.
For example:
int a=10;
int *ptra;
ptra=&a; //This statement assigns the pointer address where a is
stored .
*ptra=20; // This statement assign 20 to the memory location pointed by ptra
Note: & is the address operator also called as reference operator and * is called as deference operator
#include<stdio.h> #include<stdio.h>
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to the
address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer
ptr++
After the above operation, the ptr will point to the location 1004 because each time ptr is incremented,
it will point to the next integer location which is 4 bytes next to the current location. This operation will
move the pointer to the next memory location without impacting the actual value at the memory
location.
Arrays are closely related to pointers in C programming but the important difference between them is
that, a pointer variable can take different addresses as value whereas, in case of array it is fixed. This is
demonstrated by the following example:
Example 4:
#include <stdio.h>
int main(){
char c[4];
int i;
for(i=0;i<4;++i){
printf("Address of c[%d]=%x\n",i,&c[i]);
}
return 0;
}
Address of c[0]=28ff44
Address of c[1]=28ff45
Address of c[2]=28ff46
Address of c[3]=28ff47
Notice, that there is equal difference (difference of 1 byte) between any two consecutive elements of
array.
int arr[4];
Relation between arrays and pointers
In arrays of C programming, name of the array always points to the first element of an array. Here,
address of first element of an array is &arr[0]. Also, arr represents the address of the pointer where it is
pointing. Hence, &arr[0] is equivalent to arr.
Also, value inside the address &arr[0] and address arr are equal. Value in address &arr[0] is arr[0] and
value in address arr is *arr. Hence, arr[0] is equivalent to *arr.
Similarly,
Enter 6 numbers:
2
3
4
5
3
4
Sum=21
Task 1: Consider two arrays: x-coor[] and y-coor[]. The ith location of the two arrays stores the x
and y coordinates of a point. For example, x-coor[4] and y-coor[4] stores the x and y coordinates of forth
point.
a) Pass these two arrays to a function which prints the quadrant in which each point lies. Emphasis is on
passing array to a function.
b) Pass these two arrays to a function. The function stores the quadrant in which each point lies in
another array (which is also passed to the function). It then prints the quadrant in main function.
Emphasis is on returning array from a function.
Task 2: Modularize the following program into multiple functions. Consider a list of prices of books
represented as one dimensional array. Traverse the list and modify the prices according to the following
conditions:
(i) if price > 250, offer discount of 10%. Update the price with respect to discount.
(ii) if price > 500, offer discount of 25%. Update the price w.r.t discount.
Modularize it into following functions.
(i) readList(): Populate prices of the items.
(ii) printList(): Prints the price list.
(iii) modifyList(): Modify the prices in the list according to the conditions given above.
Identify input arguments and return types for all the functions. Also write main function where the
functions are called.