You are on page 1of 3

TRIBHUVAN UNIVERSITY

Himalaya College Of Engineering


C-Programming (BCE/BEX/BCT( I/I))
Lab-7: Pointers
Objectives:
 To deal with array.
 To be familiar with pointer, pointer arithmetic, pointer and arrays

Background Theory:
Pointer:
• A pointer is a special variable in C programming language which stores the memory
address of other variables of the same data type. As a pointer is variable, it is also
created in some memory location.
Declaration of Pointer Variable
data_type * pointer_name;
Here, data_type can be any valid C data types and pointer_name can be any valid C identifier.
Examples of Declaration of Pointer:
int *ptr;
Here ptr is a pointer variable and it is read as a pointer to integer since it can point to integer
variables.
float *fptr;
Here fptr is a pointer variable and it is read as a pointer to float since it can point to float
variables.
char *cp;
Here cp is a pointer variable and it is read as a pointer to character since it can point to
character variables.

Void Pointer:
• A void pointer is a special type of pointer . It can point to any data type, from an integer
value to a float and a string of characters.
• Using void pointer, the pointed data can not be referenced directly( i.e, *(asterisk)
operator cannot be used on them.)
• Type casting or assignment must be used to change the void pointer to a concrete data
type to which we can refer.
• Void pointer is highly preferred in dynamic memory allocation using malloc() and
calloc().
• A void pointer is a most convention way in c for storing a raw address.
Double Pointer:
• A pointer variable containing the address of another pointer variable is known as
pointer to pointer or a chain of pointers. The pointer variable that holds the address of
another variable should be declared with additional asterisk (*).
Array and Pointers:

1
Lab Task:
Write source code and output of followings.
1. Write the output of the following source code:
#include <stdio.h>
int main()
{
int x=10,y;
int *p;
p=&x;
y=*p;
printf("value of x:%d\n",x);
printf("%d is stored at %d\n",x,&x);
printf("%d is stored at %d\n",*&x,&x);
printf("%d is stored at %d\n",*p,p);
printf("%d is stored at %d\n",p,&p);
printf("%d is stored at %d\n",y,&y);
2
*p=50;
printf("\nNow value at x:%d",x);
return 0;
}

b.

#include <stdio.h>
int main()
{
int i = 5;
int *ptr1, **ptr2;
ptr1 = &i;
ptr2 = &ptr1;
printf("The value of i = %d ", i);
printf("\nThe value of ptr1 = %d ", *ptr1);
printf("\nThe value of ptr2 = %d ", **ptr2);
printf(“\n The address of i=%d”,&i);
printf(“\n The value assigned to ptr1=%d and value at address=%d”,ptr1,*ptr1);
printf(“\n The value assigned to ptr2=%d and single indirection value=%d and double indirection
value=%d”,ptr2,*ptr2,**ptr2);
return 0;
}
2. WAP to sort ‘n’ numbers and sort them in ascending order using pointer.
3. WAP to count the number of vowels present in a line of paragraph using pointer.
4. WAP to enter 5 countries name and sort them in ascending order using pointer.
5. WAP to input elements of two mXn matrix and Subtract and display the elements in
matrix order using pointer.
6. WAP to multiply two mXn and pXq matrix and display the result using pointer.

You might also like