Professional Documents
Culture Documents
Page 1 of 12
Page 2 of 12
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; }
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
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; }
Page 7 of 12
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