You are on page 1of 12

DAY 1

Problem solving session on Pointers


1. Below program demonstrates the following: 1. How to declare a pointer variable(s) 2. How to assign address into pointer variable 3. How to access the value thru pointer variable int main() { char c = 'p'; int i = 10; // pointer variable declaration char *cptr; // points to character int *iptr; // points to integer //assignment of cptr = &c; iptr = &i; pointer variables

printf("[%c][%d]\n",*cptr,*iptr); return 0; } Check out the results ..

Page 1 of 12

2. Explore what happens if integer pointer is assigned to character pointer!!!


int main() { //Declaration int i = 256; char *cptr; //Assignment using casting cptr = (char*)&i; //Result printf("[%d]\n",*cptr); return(0); } 3. Learn how OS allocates the memory for different data types using sizeof() function int main() { char c; int i; long int li; float f; double d; char *cptr; int *iptr; long int *liptr; float *fptr; double *dptr;

Page 2 of 12

printf("size of char[%d]\nsize of int[%d]\nsize of long int[%d]\n size of float[%d]\nsizeof double[%d]\n", sizeof(c),sizeof(i),sizeof(li),sizeof(f),sizeof(d) );

printf("\n\nsize of char ptr[%d]\nsize of int ptr [%d]\nsize of long int ptr[%d]\n size of float ptr[%d]\nsizeof double ptr[%d]\n " , sizeof(cptr),sizeof(iptr),sizeof(liptr),sizeof(fptr),sizeof(dptr) ); return 0; }

4.Find out the usage of const keyword

int main() { //Declaration const int i = 10; const int *cptr = &i; i = 20; *cptr = 30; //Guess what happens .. printf("i[%d] cptr[%d]\n",i,*cptr); return 0; }

Page 3 of 12

5. Understand the usage of void pointer exhibited in the following program:

int main() { char c= 'p'; int i = 10; float f = 10.5; void *vptr; vptr = &c; printf("[%c]\n",*(char*)vptr); vptr = &i; printf("[%d]\n",*(int*)vptr); vptr = &f; printf("[%f]\n",*(float*)vptr); return 0; }

Page 4 of 12

6. Following program explains the usage of incremental operator on pointers: int main() { char s[] = "protech"; char *ptr; ptr = s; // print the address of ptr printf("[%u]\n",ptr); //increment ptr by 1 ptr++ // Print the address of ptr once again printf("[%u]\n",ptr); return 0; }

7. Understand the usage of assignment operator on pointers: int main() { int a, *ptr1,*ptr2; a = 10; ptr1 = &a; ptr2 = ptr1; printf("[%d]\n",*ptr2); return 0;}

Page 5 of 12

8. In the following programs, there will be an error while compiling/ executing. Understand the error Program 1: #include<stdio.h> int main() { int a = 10; int *ptr; ptr = NULL; printf([%d]\n,*ptr); ptr = &a; printf("[%d]\n",*ptr); return 0; } Program 2: int main() { int a = 10; void *ptr = &a; printf("[%d]\n",*ptr); return 0; }

Page 6 of 12

9. Explore while loop on pointers: int main() { char company[] = "protechsoft technologies pvt ltd"; char *ptr; ptr = company; while(*ptr) printf("%c",*ptr++ ); printf("\n"); return 0; }

10. Using above concepts, write a program which copies 2 strings.

Page 7 of 12

Problem solving session on Pointers


Following programs may have ERROR(S) - Rectify & execute 1. int main() { int i = 256; unsigned char *cptr; cptr = &i; //Get the output printf("[%d]\n",*cptr); } 2. int main(){ int i = 10; int a[] = {1,2,3}; int *const ptr = &i; printf("a[%d]\n",a[0]); a = ptr; ptr++; a++ printf("a[%d]\n",a[0] ); return 0; }

Page 8 of 12

3. int main() { int i = 5; char c[] = {"ProtechSoft"}; char *ptr; ptr = c; ptr = ptr + 2; printf("[%s]\n",ptr); ptr = ptr + i; printf("[%s]\n",ptr); return 0; }

4. int main() { char c[] = {"ProtechSoft"}; char *ptr; ptr = c; ptr1 = ptr1 + 4; printf("[%s]\n",ptr1); ptr1 = ptr1 - 12; printf("[%s]\n",ptr1); return 0; }

Page 9 of 12

5. int main() { unsigned char s[] = "ProtechSoft"; char *cptr; cptr = s+6; printf("[%c]\n",*cptr); s++; printf("[%c]\n",*cptr); s--; printf("[%c]\n",*cptr); return 0; }

6. void main() { int a[] = {10,20,30,40,50}; int *ptr; ptr = a; printf("[%u]\n",ptr); ptr++; printf("[%u]\n",ptr); }

Page 10 of 12

7. void main() { int a = 10,b = 20; int *ptr1,*ptr2; ptr1 = &a; ptr2 = ptr1; if(ptr1 == ptr2) printf("Your Comments - 1\n"); ptr2 = &b; if(ptr1 < ptr2) printf("Your Comments - 2\n"); if(ptr1 > ptr2) printf("Your Comments - 3\n"); if(ptr1 != ptr2) printf("Your Comments - 4\n"); }

8. void main() { int a, *b, c[10]; a= -100; b = &a; c[0]= a; c[9] = *b + 101; c[1] = *(c+9)++; *c = *b--; printf(%d\t%d\n,*c,*(c+1)); }
Page 11 of 12

9. void main() { char a, *b, c[3]; a= 0xaa; b = &a; c[0]= a; c[9] = *b + 10; c[1] = *(c+9)++; *c = *b--; printf(%s\n,c); }

10. void main() { unsigned char a, *b, c[2]; a= 0xab; c = &a; b = *c; c[0] = *b + 10; c[1] = *(c+9)++; *c = *b--; printf(%s\n,c); }

Page 12 of 12

You might also like