pointers - amazon s3€¦ · pointers and arrays to be able to see what pointers have got to do...

37
Pointers

Upload: others

Post on 24-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers - Amazon S3€¦ · 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:

Pointers

Page 2: Pointers - Amazon S3€¦ · 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:

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.

Page 3: Pointers - Amazon S3€¦ · 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:

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

Page 4: Pointers - Amazon S3€¦ · 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:

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)

Page 5: Pointers - Amazon S3€¦ · 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:

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.

Page 6: Pointers - Amazon S3€¦ · 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:

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

Page 7: Pointers - Amazon S3€¦ · 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:

Contd..

• The output of above program would be:

• Address of i=6485

• Value of i=3

Page 8: Pointers - Amazon S3€¦ · 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:

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

Page 9: Pointers - Amazon S3€¦ · 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:

Contd..

• The output of the above program would be:

• Address of i=6485

• Value of i=3

• Value of i=3

Page 10: Pointers - Amazon S3€¦ · 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:

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

Page 11: Pointers - Amazon S3€¦ · 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:

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.

Page 12: Pointers - Amazon S3€¦ · 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:

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.

Page 13: Pointers - Amazon S3€¦ · 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:

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

Page 14: Pointers - Amazon S3€¦ · 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:

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

Page 15: Pointers - Amazon S3€¦ · 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:

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 ;

Page 16: Pointers - Amazon S3€¦ · 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:

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 ;

Page 17: Pointers - Amazon S3€¦ · 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:

Co td…

• pri tf \ Value of i=%d ,* &i ; • pri tf \ Value of i=%d ,*j ; • pri tf \ Value of i=%d ,**k ; • }

Page 18: Pointers - Amazon S3€¦ · 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:

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

Page 19: Pointers - Amazon S3€¦ · 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:

Contd..

• Value of i=3

• Value of i=3

• Value of i=3

• Value of i=3

Page 20: Pointers - Amazon S3€¦ · 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:

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();

Page 21: Pointers - Amazon S3€¦ · 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:

Contd..

• pri tf \ %u ,p ; pri tf \ %d ,*p ; }

int *fun()

{

int * i=(int *) malloc(sizeof(int));

*i=20;

return (i);

}

Page 22: Pointers - Amazon S3€¦ · 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:

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 ;

Page 23: Pointers - Amazon S3€¦ · 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:

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

Page 24: Pointers - Amazon S3€¦ · 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:

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

Page 25: Pointers - Amazon S3€¦ · 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:

Contd..

• New value in x=1004

• New value in y=2008

• New value in z=5007

Page 26: Pointers - Amazon S3€¦ · 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:

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.

Page 27: Pointers - Amazon S3€¦ · 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:

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;

Page 28: Pointers - Amazon S3€¦ · 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:

Contd..

• Subtraction of a number from a pointer.

• int i=4,*j,*k;

• j=&i;

• j=j-2;

• j=j-5;

• K=j-6;

Page 29: Pointers - Amazon S3€¦ · 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:

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

Page 30: Pointers - Amazon S3€¦ · 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:

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;

• }

Page 31: Pointers - Amazon S3€¦ · 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:

Contd..

• While(i<=n)

• {

• pri tf \ Ele e t =%d ,*j ; • i++;

• j++; /*increment pointer to point to

next location */

• }

• }

Page 32: Pointers - Amazon S3€¦ · 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:

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

Page 33: Pointers - Amazon S3€¦ · 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:

Co td…

• This means following notations are same:

• num[i]

• *(num+i)

• *(i+num)

• i[num]

Page 34: Pointers - Amazon S3€¦ · 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:

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

Page 35: Pointers - Amazon S3€¦ · 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:

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:

Page 36: Pointers - Amazon S3€¦ · 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:

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

Page 37: Pointers - Amazon S3€¦ · 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:

• int *fun()

• {

• static int x=10;

• int y=10;

• return (&y); // return (&x);

• }

• void main()

• {

• fun();

• }