Professional Documents
Culture Documents
POINTERS
Pointers
Is a variable that stores a memory address is a variable which stores the address of
another variable is a variable that points to or references a memory location in which data is stored. Is a type of variable that allows you to specify the address of a variable
int main() { int num = 18; clrscr(); cout<<"Address of variable num: "<<&num<<endl; cout<<"Value of variable num: "<<num<<endl; cout<<"The Content of the Address "<<&num<<" is "<<*(&num)<<endl; getch(); return 1; }
18
within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of".
whose reference they store. Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".
Pointers
17001 num variable ADDRESS Contents Ptr = &num Ptr = 17001 *Ptr = 18
18
int main() { int num = 18; int *ptr; clrscr(); cout<<"Address of variable num: "<<&num<<endl; cout<<"Value of variable num: "<<num<<endl; ptr = # *ptr = 16; cout<<"New value of num: "<<num<<endl; getch(); return 1; }
Pointers
17001 num variable ADDRESS Contents ptr = &num ptr = 17001 *ptr = 18
16 18
*ptr = 16
#include<iostream.h> #include<conio.h> void main() { int *ptr; int sum; clrscr(); sum=45; ptr=∑ cout<<"sum: "<<sum<<endl; cout<<"address of sum: "<<&sum<<endl; cout<<"address pointed by ptr: "<<ptr<<endl; cout<<"content of the address pointed ptr: "<<*ptr; getch(); }
#include <iostream.h> #include<conio.h> float first(float *,float *); void second(float *,float *, float *); int main () { float e,d,m; float *ptr3; clrscr(); ptr3=&m; *ptr3=first(&e,&d); cout<<e<<" "<<d<<" "<<m<<endl; second(&e,&d,&m); cout<<e<<" "<<d<<" "<<m<<endl; return 0; }
float first(float *ptr1,float *ptr2) { *ptr1 = 2.7; *ptr2 = 1.6; return (*ptr1 + *ptr2); } void second(float *ptr1,float *ptr2, float *ptr3) { *ptr1=*ptr2; *ptr2=*ptr3; *ptr3=*ptr1; }
will point to the fifth element of the array. Pointers are helpful where size of the array is unknown. Declaring the array with a size of large value wastes lot of space.
int vals[] = {4, 7, 11}; cout << vals; // displays 0x4a00 cout << vals[0]; // displays 4 Lets takes an example: int arr[]={4,7,11}; int *ptr = arr;
What is ptr + 1?
It means (address in ptr) + (1 * size of an int) cout << *(ptr+1); // displays 7 cout << *(ptr+2); // displays 11
Array notation arr[i] is equivalent to the pointer notation *(arr + i) Assume the variable definitions int arr[]={4,7,11}; int *ptr = arr; Examples of use of ++ and -ptr++; ptr--;
Sample Program Using Array #include<iostream.h> #include<conio.h> void main() { int a[100]; int i,j=0,n,*ptr; clrscr(); cout<<"Enter the elements of the array\n"; cin>>n; cout<<"Enter the array elements: "<<endl; for(i=0;i< n;i++) cin>>a[i]; cout<<"Array element are"<<endl; for(ptr=a; ptr< (a+n); ptr++) cout<<"Value of a["<<j++ << "]=" << *ptr<<endl; }
#include <iostream.h> #include<conio.h> int main() { int num[5]; int *p; int sum=0,i;
p=num;
for(i=0;i<5;i++) { cout << "Enter an integer: " << endl; cin >> *p;
sum=sum+*p;
p++; } p=num; cout << "The sum i s " << sum << endl;\ cout << "The last number is " << *(p + 4) << endl; getch(); return(0); }
Array of pointers
#include <iostream> const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << var[i] << endl; } return 0; } #include <iostream> const int MAX = 3; int main () { int var[MAX] = {10, 100, 200}; int *ptr[MAX]; for (int i = 0; i < MAX; i++) { ptr[i] = &var[i]; } for (int i = 0; i < MAX; i++) { cout << "Value of var[" << i << "] = "; cout << *ptr[i] << endl; } return 0; }
argument from within function Pointers as Function Parameters void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } swap(&num1, &num2);
Pointer to a function
Pointer to a function are perhaps on of the more confusing uses of pointers in C. Pointers to functions are not as common as other pointer uses. However, one common use is in a passing pointers to a function as a parameter in a function call. You can pass the data and the function to be used to some control function for instance. To declare a pointer to a function do: int (*pf) (); This simply declares a pointer *pf to function that returns and int. No actual function is pointed to yet.
Now f() returns an int and takes one int as a parameter. You can do things like: ans = f(5); ans = pf(5); which are equivalent.
Sample Program
float *total(float* , float* ); int main() { float a=5.0, b=9.0; float *sum; sum = total(&a,&b); cout<<"sum is "<<*sum<<endl; getch(); return 1; } float * total(float *x, float *y) { float z = *x + *y; return &z; }
{ return (a+b); }
int subtraction (int a, int b) { return (a-b); } int operation (int x, int y, int (*functocall)(int,int)) { int g; g = (*functocall)(x,y); return (g); } int main () { int m,n,x;