You are on page 1of 22

Using C++

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; }

Memory Address of variable num


17001 num variable ADDRESS Contents

18

Pointer Operator Reference operator (&)


The address that locates a variable

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".

Pointer Operator Dereference operator (*)


Pointers are said to "point to" the variable

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 = &num; *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=&sum; 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; }

2.7 1.6 4.3 1.6 4.3 1.6

Pointers and Arrays


The pointer can be set to the address of the first element of the array. For example, int age[]; int *p; p=&age; p will point to the address of the first element of the array. For example (p+4)

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.

Pointers and Arrays


Array name is base address of array

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

Pointers and Arrays


Array Access

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; }

We can also use an array of pointers to character to store a list of strings


#include <iostream> const int MAX = 4; int main () { char *names[MAX] = { Erwin", Janet", Marco", Vhern", }; for (int i = 0; i < MAX; i++) { cout << "Value of names[" << i << "] = "; cout << names[i] << endl; } return 0; }

Pointers as Function Parameters


A pointer can be a parameter. It works like a reference parameter to allow change to

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.

If we have a function int f() then we may simply write: pf = &f;


For compiler prototyping to fully work it is better to have full function prototypes for the function and the pointer to a function: int f(int); int (*pf) (int) = &f;

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; }

int addition (int a, int b)

{ 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;

int (*minus)(int,int) = subtraction;


m = operation (7, 5, addition); x = operation (7, 5, subtraction); n = operation (20, m, minus); cout <<m<<x<<n<<endl; return 0; }

You might also like