Pointer

Blog Contents

1. POINTERS:

pointers are the variable it stores the address of another variable, a pointer variable is given as *variable.

1.1 Declare int, char variable and 2 pointer variable for respected data types,assign the address. print the value of&a,aptr,*aptr,&aptr,&b, bptr,*bptr,&bptr.

#include<stdio.h>

int main()

{

int a;

char b;

int *aptr;//pointer variable for integer

char *bptr;//pointer variable for character

a=10;//declaration of integer a

b=’e’;//declaration of integer b

aptr=&a;//pointer variable stores the address of a variable

bptr=&b;

printf(“%ld\n”,sizeof(bptr));//to print the memory size

printf(“%d %p %p %p \n”,*aptr,&a,aptr,&aptr);//%p for printing

the address(hexadecimal)

printf(“%c %p %p %p \n”,*bptr,&b,bptr,&bptr);

}

1.2, Declare an array of size (5) assign its address to an integer pointer using the pointer, initialize array and pointer array.

#include<stdio.h>

int main()

{

int a[5]={1,2,3,4,5},*p,i;//array initialization along with

initialization of pointer variable

for(i=0;i<5;i++)//loop for storing address address of each

element of the array

{

p=&a[i];

printf(“%d %p\n”,*p,p);//for printing array values and the

address of each element of the array

p++;

}

}

1.3 Print your name using pointer. print the address of each element of your name.

#include<stdio.h>
void str(char a[],int size);
int main()
{
char a[8]={“priyanka”};//character array for storing name
str(a,8);//function call
}
void str(char a[],int size)
{
int i;
char *p;//character pointer variable
p=&a[0];//address of first element of the string
printf(“%s\n”,p);//by using address it prints whole string
printf(“%d\n”,sizeof(p));
for(i=0;i<8;i++)//loop for obtaining address of the each element
in the name
{
p=&a[i];
printf(“%c %p\n”,*p,p);
p++;//next location
}
}

1.4 Addition,subtraction,multiplication and division of two numbers using pointers.

#include<stdio.h>
int add(int a,int b);//prototypes of add,sub,mul,div
int sub(int a,int b);
int mull(int a,int b);
float divi(int a,int b);
int main()
{
int a,b,sum,diff,mul;
float div;
printf(“enter the two numbers:\n”);
scanf(“%d %d”,&a,&b);
sum=add(a,b);//function call for addition
printf(“sum=%d\n”,sum);
diff=sub(a,b);//function call for subtraction
printf(“difference=%d\n”,diff);
mul=mull(a,b);//function call for multiplication

printf(“xtion=%d\n”,mul);

div=divi(a,b);//function call for division
printf(“div=%f\n”,div);
}
int add(int a,int b)//for addition of two given numbers using pointers
{
int *res,*x,*y,c;
x=&a;
y=&b;
c=*x+*y;
res=&c;
return *res;
}
int sub(int a,int b)//for subtraction of two given numbers using
pointers
{
int *res2,*x2,*y2,c;
x2=&a;
y2=&b;
c=*x2-*y2;
res2=&c;
return *res2;
}
int mull(int a,int b)//for multiplication of two given numbers using
pointers
{
int *x3,*y3,*res3,c;
x3=&a;
y3=&b;
c=(*x3)*(*y3);
res3=&c;
return *res3;
}
float divi(int a,int b)//for division of two given numbers using
pointers
{
float *x4,*y4;
float *res4;
x4=&a;
y4=&b;
*res4=*x4/(*y4);
return *res4;
}

1.5 Find the average of 5 given numbers using pointers

#include<stdio.h>
float avg(float a[],int size);//function defination
int main()
{
float a[5],res;
int i;
printf(“enter the 5 numbers to find the average\n”);
for(i=0;i<5;i++)
{
scanf(“%f”,&a[i]);//user input of five numbers
}
res=avg(a,5);//function call
printf(“average=%f”,res);//printing result
}
float avg(float a[],int size)
{
float *p,*res,avg1,c=0;//declaration of pointer variable
int i;
for(i=0;i<size;i++)
{
p=&a[i];
c=c+*p;//for finding the sum of five entered numbers
p++;
}
res=&c;
avg1=*res/5;//for finding average
return avg1;
}

1.6 Swap the two given numbers using pointers

#include<stdio.h>
void swap(int *x,int *y);
int main()
{
int a,b;
printf(“enter the two numbers a and b\n”);
scanf(“%d %d”,&a,&b);//user input of two numbers
printf(“before swapping a=%d b=%d”,a,b);
swap(&a,&b);//we pass address of the variables to the function
arguments
printf(“\nafter swapping a=%d b=%d”,a,b);//printing result after
swapping
}
void swap(int *x,int *y)
{
int t;// using temporary variable we swapping the two variables
using pointers
t=*x;
*x=*y;
*y=t;
}

1.7 Write a program to take your name as input save into another array using the pointer

#include<stdio.h>
void name(char a[],int size);
int main()
{
char a[8];// declaration of input array
printf(“enter the name\n”);
scanf(“%s”,a);//user input of our name
name(a,8);//function call of array name and size
}
void name(char a[],int size)
{
char c[8],i,*p;//declaration of the new array and pointer
variable
for(i=0;i<8;i++)
{
p=&a[i];//storing the address of the each elements of the
input array
printf(“%c”,*p);//dereferencing the pointer variable to print the
each element in the array
c[i]=*p;//storing each element into new array c[i] one by
one
p++;
}
printf(“\nthe new c array is\n”);
printf(“%s”,&c[0]);// printing the c array by using string
concept
}

1.8 Find out how many vowels are there in name using pointers

 DESCRIPTION:

1)Give any name as a input to character array as a string.

2)declare the character pointer variable and feed the string address to that variable.

3)compare the given string elements one by one with the five vowels(a,A,e,E,i,I,o,O,u,U),if the elements is equal     to this letters then increment the counter variable(intialize it to zero first.

4)print the counter value as number of vowels found in this string of given name.

MEMORY MAPPING:Input character array(any input)=priyanka

ptr=&name[0](or we can use ptr=name ptr=address of first character of the given array

ptr++=(address of the next character,we keep on incrementing till end of the string(‘\0′)

*ptr=dereferencing value

the address of each character of given input is as follow

p=0xbfa36f2

r=0xbfa36f3

i=0xbfa36f4

y=0xbfa36f5

a=0xbfa36f6

n=0xbfa36f7

k=0xbfa36f8

a=0xbfa36f9

note:this address may be changing for each compilation and different pcs

 

#include<stdio.h>

int main()

{

char name[10],*ptr;//*ptr is a pointer variable

printf(“enter the name\n”);

scanf(“%s”,name);

ptr=&name[0];//for string we need not to add (&)address symbol

int vowel=0;//vowel count in the given name

int consonant=0;//number of consonant in the given name

while(*ptr!=’\0′)//we keep on comparing the each elements in the name till end of the string

{

if(*ptr==’A’||*ptr==’a’||*ptr==’E’||*ptr==’e’||*ptr==’I’||*ptr==’i’||*ptr==’O’||*ptr==’o’||*ptr==’U’||*ptr==’u’)

vowel++;//if condition to check the number of vowels in the given name

else

consonant++;

printf(“%c=%p\n”,*ptr,ptr);//to print the address of each letter

ptr++;//for next location

}

printf(“the number of vowels=%d\n the number of consonant=%d\n”,vowel,consonant);

}

1.9 Write a program to subtract two numbers using the function, pass one argument as integer pointer and another as integer,return the integer pointer to the main function.

#include<stdio.h>

int* sub(int *x,int y);//return type is

int main()

{

int a,b,*d;

printf(“enter the two numbers to find the difference\n”);

scanf(“%d %d”,&a,&b);

d=sub(&a,b);//function call

printf(“the difference value is=%d\n”,*d);//printing the difference value

}

int* sub(int *x,int y)

{

*x=*x-y;//subtraction process

return x;//returning address.

}

1.10 Pass two char array as the size of the same size into a function using a pointer, in the function write a program to compare both arrays and return  0 or 1.if they are the same return 1 and if they are not the same return 0.

 

#include<stdio.h>

int cmp(char *x,char *y);

int main()

{

char array1[10],array2[10];

int p;

printf(“enter the first character array\n”);

scanf(“%s”,array1);

printf(“enter the second character array\n”);

scanf(“%s”,array2);

p=cmp(&array1[0],&array2[0]);//function call where we pass the address of first element of both strings

if(p==1)

printf(“two strings are equal\n”);

else

printf(“two strings are not equal\n”);

}

int cmp(char *x,char *y)//receiving the adress of first elements as a pointers in function

{

while(*x==*y)//to compare 1st elements of the array initially, if it agrees then continue else return 0

{

if(*x==’\0′ || *y==’\0′)//if any one string becomes null then stop running of loop

break;

printf(“%c=%p\n”,*x,x);

x++;//next location address of first array

y++;//next location address of second array

}

if(*x==’\0′ && *y==’\0′)//if both the strings are become null at a time

return 1;

else

return 0;

}

1.11 Declare array of character pointer initialize them and print them.

#include<stdio.h>

int main()

{

char *name[10]=”priyanka”;//character array declaration

printf(“%s”,&name[0]);//or printf(“%s”,&name[0]);/*for

string only first address is required.*/

}