Pointer in C
Pointer in C
Sr No.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
Topic
Simple Program using Pointer Pointer Expressions Pointer to Pointer Pointers and 1-D Arrays Pointers and 2-D Arrays Pointers and Strings Array of Pointers Pointers and Functions Function Returning Pointers Pointers to Function Pointers and Structure
Page No.
2 4 7 8 10 13 16 18 21 23 24
1.
1.
/*Simple program to print value of variable using Address*/ main( ) { int i=3; printf("i=%d\n", i); printf("address of i = %u\n",&i); printf("value at address = %d\n",*(&i)); return 0; }
Output: i=3 address of i = 65524 value at address = 3 2. /*Simple program to print value of variable using Pointer*/ main( ) { int i=3; int *p; p=&i; printf("value of i using pointer p is i= d\n", *p); printf("address of i using pointer p is = %u\n",p); return 0; }
Output: value of i using pointer p is = 3 address of i using pointer p is = 65524 3. /* Swap the value of two variables using pointer*/ main( ) { int i=3,j=5,k; int *p,*q; p=&i; q=&j; printf("Before swapping value of i = %d and j = %d\n",*p,*q); k=*p; *p=*q; *q=k; printf("After swapping the values i= %d and j = %d\n",*p,*q); return 0; }
Output: Before swapping value of i = 3 and j = 5 After swapping the values i= 5 and j = 3 4. /* Memory allocation for pointer variables*/ main( ) { int *j,*p; char *c; float *f; printf("address of j = %u\n",&j); printf("address of p = %u\n",&p); printf("address of c = %u\n",&c); printf("address of f = %u\n",&f); return 0; }
2.
1. /* Arithmetic operators and pointers*/ main( ) { int *p,*q; int x,y,a,b,c,d; x = 20; y =10; p = &x; q = &y; a = *p + *q; b = *p - *q; c = *p * *q; d = *p / *q; printf("value of a = %d\n",a); printf("value of b = %d\n",b); printf("value of c = %d\n",c); printf(value of d = %d\n",d); return 0; } Output: value of a = 30 value of b = 10 value of c = 200 value of d = 2 2. /* Arithmetic Expressions and pointers*/ main( ) { int *p,*q; int i,x,y,z; int a,b,c,d; x=20; y=10; p=&x; q=&y; a= ++(*p) + *q; b= ++ *p+ *q; c= *p++ + *q; d= *p + *q++; printf("value of a = %d\n",a); printf("value of b = %d\n",b); printf("value of c = %d\n",c); printf("value of d = %d\n",d); printf("value of x = %u\n",*p); printf("value of y = %u\n",*q); return 0; }
Pointer Expressions
Output: value of a = 31 value of b = 32 value of c = 32 value of d = 15 value of x = 5 value of y = 22 3. /* Subtraction of Pointers*/ main( ) { int *p,*q; char *c; float *f; int x,y; char ch; float f1; p=&x; q=&y; c=&ch; f=&f1; printf("address of x(p) = %u\naddress of y(q) = %u\naddress of ch(c) = %u\naddress of f1(f) = %u\n",p,q,c,f); printf("subtraction of address [p-q]= %d\n",p-q); printf("subtraction of address [q-p]= %d\n",q-p); printf("subtraction of address [p-c]= %d\n",p-c); printf("subtraction of address [c-p]= %d\n",c-p); printf("subtraction of address [q-c]= %d\n",q-c); printf("subtraction of address [c-q]= %d\n",c-q); printf("subtraction of address [p-f]= %d\n",p-f); printf("subtraction of address [f-p]= %d\n",f-p); printf("subtraction of address [q-f]= %d\n",q-f); printf("subtraction of address [f-q]= %d\n",f-q); printf("subtraction of address [c-f]= %d\n",c-f); printf("subtraction of address [f-c]= %d\n",f-c); return 0; }
Output: address of x(p) = 65520 address of y(q) = 65518 address of ch(c) = 65517 address of f1(f) = 65512 subtraction of address [p-q]= 1 subtraction of address [q-p]= -1 subtraction of address [p-c]= 1 subtraction of address [c-p]= -3 subtraction of address [q-c]= 0 subtraction of address [c-q]= -1 subtraction of address [p-f]= 4 5
subtraction of address [f-p]= -2 subtraction of address [q-f]= 3 subtraction of address [f-q]= -1 subtraction of address [c-f]= 5 subtraction of address [f-c]= -1
3.
1. /* Access the value of variable using pointer to pointer*/ main( ) { int x; int *p int **q; x=10; p = &x; q = &p; printf("value of x = %d\n",**q); printf("Addess of x = %u\n",*q); return 0; }
Pointer to Pointer
4.
1. /* Access the element of 1-D array using pointer*/ main( ) { int i,j,t,n; int a[100],*p; printf("enter the size of array:"); scanf("%d",&n); p=a; for(i=0;i<n;i++) { printf("enter the element of array:"); scanf("%d",p+i); } for(i=0;i<n;i++) { printf("%d\n",*(p+i)); } return 0; } Output: enter the size of array: 5 enter the element of array:1 enter the element of array:2 enter the element of array:3 enter the element of array:4 enter the element of array:5 1 2 3 4 5 2. /* Sorting of 1-D array using pointer*/ main( ) { int i,j,t,n; int a[100],*p; printf("enter the size of array:"); scanf("%d",&n); p=a; for(i=0;i<n;i++) { printf("enter the element of array:"); scanf("%d",p+i); }
for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(*(p+j) > *(p+j+1)) { t=*(p+j); *(p+j)=*(p+j+1); *(p+j+1)=t; } } } for(i=0;i<n;i++) { printf("%d\n",*(p+i)); } return 0; }
Output: enter the size of array: 5 enter the element of array:12 enter the element of array:5 enter the element of array:36 enter the element of array:25 enter the element of array:7 5 7 12 25 36 Exercise 1. Write a program to sort an array of 10 elements in descending order using pointer. 2. Write a program to find maximum number from element of an array using pointer. 3. Write a program to find total of all elements of an array using pointer. 4. Write a program to reverse a given array of 20 elements using pointer. 5. Write a program to entered sorted array and integer value and insert a new entered value in correct place using pointer.
5.
1. /* Access the element of 2-D array using pointer*/ main( ) { int i,j,k,m,n,l; int a[10][10]; printf("enter the order of a:"); scanf("%d %d",&m,&n); printf("enter the m x n elements of a:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",(*(a+i)+j)); } } printf("element of a matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",*(*(a+i)+j)); } printf("\n"); } return 0; } Output: enter the order of a:3 3 enter the m x n elements of a: 1 2 3 4 5 6 7 8 9 element of a matrix is: 1 2 3 4 5 6 7 8 9
10
2. /* Matrix multiplication using pointer*/ #include<stdio.h> main( ) { int i,j,k,m,n,l; int a[10][10],b[10][10],c[10][10],d[10][10]; printf("enter the order of a:"); scanf("%d %d",&m,&n); printf("enter the 9 elements of a:\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",(*(a+i)+j)); printf("enter the order of b:"); scanf("%d %d",&n,&l); printf("enter the 9 elements of b:\n"); for(i=0;i<n;i++) for(j=0;j<l;j++) scanf("%d",(*(b+i)+j)); printf("elemet of a matrix is:\n"); for(i=0;i<m;i++){ for(j=0;j<n;j++){ printf("%d\t",*(*(a+i)+j));} printf("\n");} printf("elemet of b matrix is:\n"); for(i=0;i<n;i++){ for(j=0;j<l;j++){ printf("%d\t",*(*(b+i)+j));} printf("\n");} for(i=0;i<m;i++){ for(j=0;j<l;j++){ *(*(d+i)+j)=0; for(k=0;k<n;k++) { *(*(d+i)+j) = *(*(d+i)+j) + *(*(a+i)+k) * *(*(b+k)+j); }} printf("\n");} printf("resultant matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",*(*(d+i)+j)); } printf("\n"); } return 0; }
11
Output: enter the order of a:3 3 enter the m x n elements of a: 1 2 3 4 5 6 7 8 9 enter the order of b:3 3 enter the m x n elements of b: 1 1 1 1 1 1 1 1 1 element of a matrix is: 1 2 3 4 5 6 7 8 9 element of b matrix is: 1 1 1 1 1 1 1 1 1 resultant matrix is: 6 6 6 15 15 15 24 24 24 Exercise 1. Write a program for addition of two matrices using pointer. 2. Write a program to find transpose of given 3x3 matrix using pointer. 3. Write a program to find whether two matrices are identical or not using pointer. 4. Write a program to find where a given matrix is orthogonal or not using pointer. 5. Write a program to find the inverse of matrix using pointer.
12
6.
1. /* Read and print the string using pointer*/ main( ) main( ) { { char char name[6]="DELHI"; name[6]="DELHI"; char *p; printf("%s\n",name); p=name; return 0; printf("%s\n",p); } while(*p!=NULL) { Output: DELHI printf("%c\n",*p); p++; } return 0; main( ) } { char name[6]; scanf("%s\n",name); printf("%s\n",name); return 0; } Output: DELHI DELHI Output: DELHI D E L H I Output: DELHI D E L H I
2. /* Find the length of string using pointer*/ main( ) { char name[6]="DELHI"; int length; char *p; char *q; p=name; q=p; printf("%s\n",name); while(*p!=NULL) { printf("%c is stored at address %u\n",*p,p); p++; } length = p - q; printf("\n Length of the string = %d\n",length); return 0; }
13
Output: DELHI D is stored at address 176 E is stored at address 177 L is stored at address 178 H is stored at address 179 I is stored at address 180 Length of the string = 5 3. /*read and print 2- D array of characters*/ main( ) main( ) { { char a[3][25]; char int i,j; a[3][25]={"DELHI","BOMBAY","KOLKATA"}; for(i=0;i<3;i++) int i,j; { for(i=0;i<3;i++) scanf("%s",a[i]); { } printf("%s\n",a[i]); // *(a+i) } for(i=0;i<3;i++) for(i=0;i<3;i++) { { printf("%s\n",a[i]); j=0; } while(*(*(a+i)+j)!=NULL) return 0; { } printf("%c",*(*(a+i)+j)); // (*(a[i]+j)) j++; } printf("\n"); } return 0; } Output: DELHI Output: BOMBAY DELHI KOLKATA BOMBAY KOLKATA DELHI BOMBAY DELHI KOLKATA BOMBAY KOLKATA
14
Exercise 1. 2. 3. 4. 5. Write a program to print a string in reverse order using pointer. Write a program to print a string in alphabetical order using pointer. Write a program to find length of 5 entered string using pointer. Write a program to find whether a string is palindrome or not using pointer. Write a program to find longest word from enter string and also find length of longest word using pointer. Write a program to enter a character and a string, find the occurrence of that character in entered string and also delete that character using pointer.
6.
15
7.
1.
Array of Pointers
/* Access the number of string having different length using array of pointers*/ main( ) main( ) { { char char *a[3]={"abcd","ab","abc"}; *a[3]={"abcd","ab","abc"}; int i,j; int i; for(i=0;i<3;i++) for(i=0;i<3;i++) { { j=0; printf("%s\n",*(a+i)); while((*(a[i]+j)!=NULL)) // (*(*(a+i)+j)) } { return 0; printf("%c",* (*(a+i)+j)); } j++; } 2. printf("\n"); } return 0; } Output: Output: abcd abcd ab ab abc abc
2. /*determine the length of each and every string using array of pointers*/ main( ) { char *a[3]={"abcd","ab","abc"}; int i,j,count; for(i=0;i<3;i++) { j=0; count=0; while((*(a[i]+j)!=NULL)) { count++; j++; } printf("length of [%d] string is : %d\n",i,count); } return 0; }
Output: 16
Exercise
1. Write a program to find length of 5 entered string using array of pointers. 2. Write a program to find whether 5 entered strings are palindrome or not using array of pointers. 3. Write a program to arrange the strings (3 entered strings) according to its length using array of pointers. 4. Write a program to arrange the strings (3 entered strings) alphabetical order using array of pointers.
17
8.
1.
/*To pass pointers as function arguments and function does not returns anything to main*/ main( ) { void swap(int *,int *); int a,b; a=10; b=20; printf("Before swapping a = %d b = %d\n",a,b); swap(&a,&b); printf("After swaping a = %d b = %d",a,b); return 0; } void swap(int *x,int *y) { int temp; temp = *x; *x=*y; *y=temp; }
Output: Before swapping a = 10 b = 20 After swapping a = 20 b = 10 2. /*To add two integers numbers and results must be returns to main using pointers as function arguments*/ main( ) { int add(int *,int *); int a,b,c; a=10; b=20; c = add (&a,&b); printf("value of c = %d",c); return 0; } int add(int *x,int *y) { int z; z = *x + *y; return z; } Output: Value of c = 30
18
3. /*To sort array element in ascending order using function sort ( ) and pass pointers as function arguments*/ main( ) { int a[100],i,n; void sort(int,int *); int *p,*q; printf("enter the size of array:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the numbers:\n"); scanf("%d",&a[i]); } p=a; sort(n,p); return 0; } void sort(int x,int *y) { int i,j,temp,*z; z=y; for(i=0;i<x-1;i++) { for(j=0;j<x-i-1;j++) { if(*(y+j) > *(y+j+1)) { temp = *(y+j); *(y+j) = *(y+j+1); *(y+j+1) = temp; } } } for(i=0;i<x;i++) { printf("value of %d the element is %d\n",i,*z); z++; } }
Output: enter the size of array: 5 enter the numbers: 12 enter the numbers: 23 enter the numbers: 5 enter the numbers: 34 enter the numbers: 10 value of 0 the element is 5 value of 1 the element is 10 value of 2 the element is 12 19
20
9.
1. /* Example of function retuning pointer */ main( ) { int *large(int *,int *); int a,b,*c; a=10; b=20; c = add (&a,&b); printf("value of c = %d",*c); return 0; Output: } Value of c = 20 int *large(int *x,int *y) { int *z; if(*x > *y) return x; else return y; } Output: value of c =20 2.
/* Sorting of array using function retuning pointer */ main( ) { int a[100],i,n; int *sort(int,int *); int *p,*q; printf("enter the size of array:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the numbers:\n"); scanf("%d",&a[i]); } p=a; q=sort(n,p); for(i=0;i<n;i++) { printf("value of %d the element is %d\n",i,*q); q++; } return 0; }
21
int *sort(int x,int *y) { int i,j,temp,*z; z=y; for(i=0;i<x-1;i++) { for(j=0;j<x-i-1;j++) { if(*(y+j) > *(y+j+1)) { temp = *(y+j); *(y+j) = *(y+j+1); *(y+j+1) = temp; } } } return z; } Output: enter the size of array: 5 enter the numbers: 12 enter the numbers: 23 enter the numbers: 5 enter the numbers: 34 enter the numbers: 10 value of 0 the element is 5 value of 1 the element is 10 value of 2 the element is 12 value of 3 the element is 23 value of 4 the element is 34
22
10.
1. /* Simple program of pointer to function*/ main( ) { int a,b,c; int mul(int,int); int (*p)( ); p=mul; printf("enter the two numbers:\n",a,b); scanf("%d %d",&a,&b); c = (*p)(a,b); printf("\n multiplication is = %d\n",c); return 0; } int mul(int x,int y) { int z; z= x*y; return z; }
Pointers to Function
23
11.
1. /* Simple program of pointer and structures*/ struct student { int number; int marks; char name[25]; }st,*p;
main( ) { p=&st; printf("enter the data:"); //scanf("%d %d %s",&p->number,&p->marks,p->name); //printf("%d %d %s",p->number,p->marks,p->name); scanf("%d %d %s",&(*p).number,&(*p).marks,(*p).name); printf("%d %d %s",(*p).number,(*p).marks,(*p).name); return 0; }
Output: enter the data :1 12 abc 1 12 abc 2. /* Simple program of pointer and structures and array of stuctures*/ struct student { int number; int marks; char name[25]; }st[2],*p; main( ) { for(p=st;p<st+2;p++) { printf("enter the data:"); scanf("%d %d %s",&p->number,&p->marks,p->name); } for(p=st;p<st+2;p++) { printf("%d %d %s\n",p->number,p->marks,p->name); } return 0; } 24
Output: enter the data :1 12 abc enter the data :2 24 xyz 1 12 abc 2 24 xyz 3. /* Simple program of pointer and structures, array of structures and array within structures*/ struct student { int number; int marks[3]; char name[25]; }st[2],*p,*q; main( ) { int i,j; p=st; for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("enter the marks of three subjects:"); scanf("%d",&p->marks[j]); } printf("enter the number and name:"); scanf("%d %s",&p->number,p->name); p++; } p=st; for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("%d\n",p->marks[j]); } printf("%d %s \n",p->number,p->name); p++; } return 0; }
25
Output: enter the marks of three subjects:12 13 14 enter the number and name: 1 abc enter the marks of three subjects:20 30 40 enter the number and name: 2 xyz
12 13 14 1 abc 20 30 40 2 xyz 4. /* Simple program of pointer and structures, pointer within structures*/ struct student { int *number; int marks; char name[25]; }*p; main( ) { struct student st[2]; int z=5,x=20,y=10; p=st; st[0].number =&x; st[1].number = &y; printf("%u %u %u\n",&x,&y,&z); printf(" %u %d\n",p->number,*p->number); return 0; }
26
5. /* Simple program of pointer and structures, array of structures and function and structures*/ struct student { int number; int marks[3]; char name[25]; int total; }st[2],*p,*q; main( ) { int i,j; void total1(struct student *); p=st; for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("enter the marks of three subjects:"); scanf("%d",&p->marks[j]); } printf("enter the number and name:"); scanf("%d %s",&p->number,p->name); p++; } p=st; for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("%d\n",p->marks[j]); } printf("%d %s\n",p->number,p->name); p++; } p=st; total1(p); return 0; } void total1(struct student *r) { int i,j; for(i=0;i<2;i++) { r->total =0; for(j=0;j<3;j++) { r->total = r->total+r->marks[j]; } printf("total = %d\n",r->total); r++; } }
27
Output: enter the marks of three subjects:10 20 30 enter the number and name: 1 abc enter the marks of three subjects:20 30 40 enter the number and name: 2 xyz total = 60 total = 90 6. /* Simple program of pointer and structures, array of structures and array within structures, function returning pointer*/ struct student { int number; int marks[3]; char name[25]; int total; }st[2],*q,*x; main( ) { int i,j; struct student *total1(struct student * ); for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("enter the marks of three subjects:"); scanf("%d",&st[i].marks[j]); } printf("enter the number and name:"); scanf("%d %s",&st[i].number,st[i].name); } for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("%d\n",st[i].marks[j]); } printf("%d %s\n",st[i].number,st[i].name); } q=total1(st); for(i=0;i<2;i++) { printf("%d\n",q->total); q++; } return 0; }
28
struct student *total1(struct student *r) { int i,j; r->total = 0; x=r; for(i=0;i<2;i++) { for(j=0;j<3;j++) { r->total = r->total+r->marks[j]; } r++; } return x; }
Output: enter the marks of three subjects:10 20 30 enter the number and name: 1 abc enter the marks of three subjects:20 30 40 enter the number and name: 2 xyz total = 60 total = 90
29