Pointers is the heart of c programming . pointers may lead to very fast and efficient programs of they are used correctly ,or may lead very buggy and faulty programs if they are not.
Pointer is a variable used to store the address of another variable for that reason it can be used to read or change the pointed value indirectly.
Define a pointer :
Pointer Types :
Example 1 : Structure And Pointer.
Address is the location of variable or function in memory .when the program is loaded all variables and functions are placed in the main memory.When the program tries to read a value from a variable ,it actually goes the variable address and brings the required value.
Example 2 : Printing Variables Address and Values.
Simply Pointer is a special type of variables used to store the address value.
Example 4 : Pointer To Variable.
Example 5 : Using Pointer To Variable.
Example 7 : Using Pointer To Array
Example 9 : Pointer To Function.
Pointer is a variable used to store the address of another variable for that reason it can be used to read or change the pointed value indirectly.
Define a pointer :
type *pointer-name;
Pointer Types :
int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */
Example 1 : Structure And Pointer.
#include <stdio.h> #include <stdlib.h> struct date { int day; int month; int year; }; struct student_data { char name[20]; int age; struct date graduation; float gpa; }; void printstudent (struct student_data *x) { printf("Student Name:%s\nStudent Age:%d\nStudent Graduation Data:%d/%d/%d\nStudent GPA:%f\n" ,x->name,x->age,x->graduation.day,x->graduation.month,x->graduation.year,x->gpa); } int main() { struct student_data a={"Max John",23,{9,10,2014},3.51}; printstudent(&a); return 0; }
Address is the location of variable or function in memory .when the program is loaded all variables and functions are placed in the main memory.When the program tries to read a value from a variable ,it actually goes the variable address and brings the required value.
Example 2 : Printing Variables Address and Values.
#include <stdio.h> #include <stdlib.h> int main() { int x=3 , y=8 ,z; z=x+y; printf("x at location address %x contains %d\n",&x,x); printf("y at location address %x contains %d\n",&y,y); printf("z at location address %x contains %d\n",&z,z); }
Array is a set of arranged variables placed at one location.In C language array name is beginning address of the whole array.Also every element in the array have a unique address.
Example 3 : Printing Array and Array Element Address :
#include <stdio.h> #include <stdlib.h> int main() { int x[5]={0,1,2,3,4,5}; int i; printf("x Array at location address %x\n",&x); for(i=0;i<5;i++){ printf("%d is located at address %d\n",x[i],&x[i]); } }
Example 4 : Pointer To Variable.
#include <stdio.h> #include <stdlib.h> int main() { int x=5; int *ptr; ptr=&x; printf("Pointer Address is %x , X Address is %x\n",ptr,&x); printf("Pointer Value is %d , X Value is %d\n",*ptr,x); }
Example 5 : Using Pointer To Variable.
#include <stdio.h> #include <stdlib.h> int main() { int x,y,z; int *ptr; ptr=&x; printf("Please Enter The First Integer :\n"); scanf("%d",ptr); ptr=&y; printf("Please Enter The Second Integer :\n"); scanf("%d",ptr); z=x+y; printf("Addition Result=%d\n",z); }
Example 6 : Pointer To Array.
#include <stdio.h> #include <stdlib.h> int main() { int x[5]={0,1,2,3,4}; int *ptr=x; printf("%d\n",*ptr); ptr++; printf("%d\n",*ptr); ptr=x+3; printf("%d\n",*ptr); ptr--; printf("%d\n",*ptr); }
Example 7 : Using Pointer To Array
#include <stdio.h> #include <stdlib.h> int main() { int x[5]={5,4,1,10,5}; int *ptr=x; int sum=0,average,i; for (i=0;i<4;i++){ sum+=*ptr; ptr++; } average=sum/5; printf("Average=%d\n",average); }
Example 8: Pointer To Structure.
#include <stdio.h> #include <stdlib.h> struct person { char name[20]; int age; int id; int salary; }; int main() { int i; struct person manager={"Sayed Ali",56,700345,37000}; struct person emploee[]={{"Max John",31,846533,20000}, {"Ahmed Ali",36,875543,25000}}; struct person *ptr; ptr=&manager; printf("Manger Name :%s\nAge:%d\nID:%d\nSalary:%d\n",ptr->name, ptr->age,ptr->id,ptr->salary); ptr=emploee; for(i=0;i<sizeof(emploee)/sizeof(struct person);i++,ptr++){ printf("Emploee Name:%s\nAge:%d\nID:%d\nSalary:%d\n",ptr->name, ptr->age,ptr->id,ptr->salary); } }
Pointer are used efficiently with function.Using pointer provide two main feature.
1.Fast data transfer , because only pointer are transferred.
2.Pointers allow the definition of several outputs for the same function.
#include <stdio.h> #include <stdlib.h> struct person { char name[20]; int age; int id; int salary; }; int addsalary(struct person *ptr){ int sum=0,i; for(i=0;i<3;i++){ sum+=ptr->salary; /* Add All Salary Of emploee By Using Pointer Passing on Them*/ ptr++; } return sum; }; int main() { int i,add; struct person emploee[3]={{"Max John",31,846533,20000}, {"Ahmed Ali",36,875543,25000}, {"Sara Salah",35,875543,23500}}; struct person *ptr; ptr=emploee; /* Ptr Stand on The Fist Emploee before Print */ for(i=0;i<3;i++,ptr++){ printf("Emploee Name:%s\nAge:%d\nID:%d\nSalary:%d\n",ptr->name, ptr->age,ptr->id,ptr->salary); } ptr=emploee; /* Ptr Stand on The Fist Emploee before Go to the Function */ add=addsalary(ptr); printf("ToTal Salaries =%d\n",add); }
Example 10 : Return more Than Variable From Function By Using Pointers.
#include <stdio.h> #include <stdlib.h> int MathFunction( int a , int b ,int *add , int *sub , int *mult , int *div){ *add=a+b; *sub=a-b; *mult=a*b; *div=a/b; }; int main() { int x,y,addtion,subtraction,multiplication,division; printf("Please Enter Two Integer Value :\n"); scanf("%d%d",&x,&y); MathFunction(x,y,&addtion,&subtraction,&multiplication,&division); printf("Addition=%d\nSubtraction=%d\nMultiplication=%d\ndivision=%d\n", addtion,subtraction,multiplication,division); }
Example 11 :Passing Arrays and Pointers To Function.
#include <stdio.h> void disp( int *num) { printf("%d ", *num); } int main() { int i,arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; for (i=0; i<=10; i++) { /* I’m passing element’s address*/ disp (&arr[i]); } return 0; }
Example 12 : Pointer With Unknown Type (Void).
#include <stdio.h> #include <stdlib.h> int main () { int x=6; double y=1.3; void *ptr ; ptr=&x; *(int*)ptr=9; printf("Value of x is %d\n",x); ptr=&y; *(double*)ptr=10.5; printf("Value of Y is %lf\n",y); return 0; }
Example 13 : Pointer To Pointer.
#include <stdio.h> #include <stdlib.h> int main () { int x=10 ,y=5; int *ptr1=&x; int **ptr2=&ptr1; printf(" x=%d y=%d \n",x,y); **ptr2=7; printf(" x=%d y=%d \n",x,y); *ptr2=&y; *ptr1=11; printf(" x=%d y=%d \n",x,y); return 0 ; }
(int *ptr1=&x) : means defining a pointer (ptr1) and assign to it the address of x.
(int **ptr2=&ptr1) : means defining a pointer to pointer (ptr2) and assign to it the address of ptr2.
(**ptr2=7): means writing a value (7) to x. This happens because ptr2 point to ptr1 and ptr1 point to x.
(*ptr2=&y): means altering the address value in ptr1 with the address of y.This happens because ptr2 is point to ptr1.
(*ptr1=11): means writing a value of (11) to y. This happen because in previous step ptr1 is pointed to y.
Example 14 : NULL and Unassigned Pointer.
If pointer is unassigned it will contain an invalid address,it is unsafe to use an unassigned pointer,normally the program will crash.Following program will crash because the ptr pointer is not pointed to a valid address,it contain a memory garbage.
#include <stdio.h> #include <stdlib.h> void main () { int *ptr; printf("Ptr Point to %d",*px); }
To avoid using unassigned pointer,all pointer must hold a valid address,if not it must hold a zero value.Sometimes zero value called (NULL).Above program may be fixed as shown below.
#include <stdio.h> #include <stdlib.h> void main () { int *ptr=NULL; if(ptr !=NULL){ printf("Ptr Point to %d",*px); } else printf("ptr is not initialized"); }
Awesome post
ReplyDelete