You are on page 1of 10

Programming Fundamentals

Lab
Ms. Ruqiya
ruqiyaabbasi42@gmail.com
Write a code to Creating Pointers

 #include <stdio.h>

 int main() {
 int X = 43;

 printf("%d\n", X);
 printf("%p\n", &X);
 return 0;
 }
Cont…

 #include <stdio.h>

 int main() {
 int X = 43;
 int* ptr = &X;
 printf("%d\n", X);
 printf("%p\n", &X);
 printf("%p\n", ptr);

 return 0;
 }
Write a code on pointer

 #include <stdio.h>
 int main()
 {
 int var = 5;
 printf("var: %d\n", var);

 // Notice the use of & before var


 printf("address of var: %p", &var);
 return 0;
 }
Example: Working of Pointers

 #include <stdio.h>
 int main()
 {
 int* pc, c;
 c = 22;
 printf("Address of c: %p\n", &c);
 printf("Value of c: %d\n\n", c); // 22
 pc = &c;
 printf("Address of pointer pc: %p\n", pc);
 printf("Content of pointer pc: %d\n\n", *pc); // 22
 c = 11;
 printf("Address of pointer pc: %p\n", pc);
 printf("Content of pointer pc: %d\n\n", *pc); // 11
 *pc = 2;
 printf("Address of c: %p\n", &c);
 printf("Value of c: %d\n\n", c); // 2
 return 0;
 }
Null Pointer

 #include<stdio.h>
 int main()

 {

 int *var = NULL;

 printf("var=%d",*var);

 }
Void Pointer

 #include<stdio.h>

 int main()

 {

 int a=2;

 void *ptr;

 ptr= &a;

 printf("After Typecasting, a = %d", *(int *)ptr);

 return 0;

 }
Wild Pointer

 #include<stdio.h>

 int main()

 {

 int *ptr;

 printf("ptr=%d",*ptr);

 return 0;

 }
Dangling Pointer

 #include<stdio.h>
 #include<stdlib.h>

 int main()

 {
 int *ptr=(int *)malloc(sizeof(int));

 int a=5;

 ptr=&a;

 free(ptr);

 //now this ptr is known as dangling pointer.

 printf("After deallocating its memory *ptr=%d",*ptr);

 return 0;

 }
Thank You!

You might also like