Pointers
Pointers
Pointers
• Features of C do beginners find most difficult
to understand?
• The difficulty beginners have with pointers has
u h to do ith C s poi ter ter i olog tha the actual concept.
The & and *operators
• Consider the declaration
• int i=3;
• This declaration tells the C compiler to:
• i) Reserve space in memory to hold integer
value.
• ii) Associate the name i with this memory
location.
• Iii)Store the value 3 at this location
Co td…
• We a represe t i s lo atio i the e or by the following memory map:
•
• 3
6485
i Location name
Value at
location
Location
no(Address)
Contd..
• We see that the computer has selected
memory location 6485 as the place to store
the value 3.This location number 6485 is not a
number to be relied upon , because some
other time the computer may choose a
different location for storing the value 3.
Contd..
• We can print this address through the
following program:
• main()
• {
• int i=3;
• pri tf \ Address of i=%u ,&i ; • pri tf \ Value of i=%d ,i ; • }
Contd..
• The output of above program would be:
• Address of i=6485
• Value of i=3
Contd..
• Observe carefully the output of the following
program:
• main() {
• int i=3;
• pri tf \ Address of i=%u ,&i ; • pri tf \ Value of i=%d ,i ; • pri tf \ Value of i=%d ,* &i ; • }
Contd..
• The output of the above program would be:
• Address of i=6485
• Value of i=3
• Value of i=3
Pointer Expressions
• &i address of i
• j=&i j is a variable the compiler must
provide it space in memory.
6485 3
6485 3276
i j
Co td….
• i alue is a d j s alue is i s address. • But e a t use j i a progra ithout
declaring it.
• int *j;
• This declaration tells the compiler that j will
be used to store the address of an integer
value –in other words j points to an integer.
Contd..
• Let us go by the meaning of *.
• It stand for value at address .Thus int *would
mean ,the value at the address contained in j
is in int.
Contd..
• Here is a program that demonstrates the
relationships we have been dicussing:
• Main()
• {
• int i=3;
• int *j;
• j=&i;
• pri tf \ address of i=%u ,&i ; •
Contd..
• pri tf \ Address of i=%u ,j ; • pri tf \ Address of j=%u ,&j ; • pri tf \ Value of j=%d ,j ; • pri tf \ Value of i=%d ,i ; • pri tf \ Value of i=%d ,* &i ; • pri tf \ Value of i=%d ,*j ; • }
Contd..
• main()
• {
• int i=3;
• int *j;
• int **k;
• j=&i; //6485
• k=&j; //address of j =3276 and k=7234
• pri tf \ address of i=%u ,&i ;
Contd..
• pri tf \ address of i=%u ,j ; • pri tf \ address of i=%u ,*k ; • pri tf \ address of j=%u ,&j ; • pri tf \ address of j=%u ,k ; • pri tf \ address of k=%u ,&k ; • pri tf \ Value of j=%u ,j ; • pri tf \ Value of k=%u ,k ; • pri tf \ Value of i=%d ,i ;
Co td…
• pri tf \ Value of i=%d ,* &i ; • pri tf \ Value of i=%d ,*j ; • pri tf \ Value of i=%d ,**k ; • }
Co td…
• The output of the above program would be:
• Address of i=6485
• Address of i=6485
• Address of i=6485
• Address of j=3276
• Address of j=3276
• Address of k=7234
• Value of j=6485
• Value of k=3276
Contd..
• Value of i=3
• Value of i=3
• Value of i=3
• Value of i=3
Functions Returning pointers
The way functions return an int,float, a
double or any other data type , it can even
return pointer.
The following program illustrates this.
main()
{
int *p;
int *fun();
p=fun();
Contd..
• pri tf \ %u ,p ; pri tf \ %d ,*p ; }
int *fun()
{
int * i=(int *) malloc(sizeof(int));
*i=20;
return (i);
}
Pointers and Arrays
• To be able to see what pointers have got to do
with arrays , Let us first learn some pointer
arithmetic. Consider the following example:
• main(){
• int i=3,*x;
• float j=1.5,*y;
• Char k= ,*z; • pri tf \ Value of i=%d ,i ;
Contd..
• pri tf \ Value of i=%d ,i ; • pri tf \ Value of j=%f ,j ; • pri tf \ Value of k=% ,k ; • x=&i;
• y=&j;
• z=&k;
• pri tf \ origi al alue i =%u , ; • pri tf \ origi al alue i =%u , ; • pri tf \ origi al alue i z=%u ,z ; • }
Contd..
• Suppose i,j and k are stored in memory at addresses
, a d ,the output ould e…. • Value of i=3
• Value of j=1.500000
• Value of k=c
• Original value in x=1002
• Original value in y=2004
• Original value in z=5006
Contd..
• New value in x=1004
• New value in y=2008
• New value in z=5007
Contd..
1004 is equal to original value in x plus 2 .
2008 is equal to original value in y plus 4.
5007 is equal to original value in z plus 1.
The way a pointer can be incremented ,it can
be decremented as well ,to point to earlier
locations. Thus the following operations can
be performed on a pointer.
Contd..
• Addition of number to a pointer. For example
• int i=4,*j,*k;
• j=&i;
• j=j+1;
• j=j+9;
• K=j+3;
Contd..
• Subtraction of a number from a pointer.
• int i=4,*j,*k;
• j=&i;
• j=j-2;
• j=j-5;
• K=j-6;
Co td…
• Do not attempt these operations on pointers
• Addition of two pointers
• Multiplying a pointer with a number
• Dividing a pointer with a number
Contd..
• Passing an Entire Array to a Function
• main(){
• int num[]={24,34,12,44,56,17};
• display(&num[0],6);
• }
• display(int *j , int n)
• {
• int i=1;
• }
Contd..
• While(i<=n)
• {
• pri tf \ Ele e t =%d ,*j ; • i++;
• j++; /*increment pointer to point to
next location */
• }
• }
Co td…
• Int num[]={23,34,12,44,56,17};
• We already know that mentioning the name
of the array we get its base address. Thus, by
saying *num we would be able to refer to the
zeroth element of the array.
• *(num+0) refer to 23
• *(num+1) refer to 34
• num[i] ,C compiler internally converts it to
*(num+i).
Co td…
• This means following notations are same:
• num[i]
• *(num+i)
• *(i+num)
• i[num]
Contd..
• /*Accessing array elements in different ways *
• main() {
• int num[]={24,34,12,44,56,17};
• int i=0;
• While(i<=5)
• {
• pri tf \ address =%u ,& u [i] ; • pri tf ele e t =%d , u [i] ; • }
•
Contd..
• pri tf %d ,* u +i ; • pri tf %d ,* i+ u ; • pri tf %d ,i[ u ] ; • }
• The output of the program would look like
this:
Contd..
• Address =4001 element 24 24 24 24
• Address =4003 element 34 34 34 34
• Address =4005 element 12 12 12 12
• Address =4007 element 44 44 44 44
• Address =4009 element 56 56 56 56
• Address =4011 element 17 17 17 17
• int *fun()
• {
• static int x=10;
• int y=10;
• return (&y); // return (&x);
• }
• void main()
• {
• fun();
• }