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);
1.5 Find the average of 5 given numbers using pointers
1.6 Swap the two given numbers using pointers
1.7 Write a program to take your name as input save into another array using the pointer
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.*/
}