Introduction to Pointers in Programming

Introduction to Pointers in Programming
paly

This article explains pointers in programming, including their data type, how to declare and use them, and their relationship with computer memory.

About Introduction to Pointers in Programming

PowerPoint presentation about 'Introduction to Pointers in Programming'. This presentation describes the topic on This article explains pointers in programming, including their data type, how to declare and use them, and their relationship with computer memory.. The key topics included in this slideshow are pointers, programming, data types, memory, variables,. Download this presentation absolutely free.

Presentation Transcript


1. Senem KUMOVA METN CS115 2008-2009 FALL 1 POINTERS && ARRAYS CHAPTER 6

2. Senem KUMOVA METN CS115 2008-2009 FALL 2 POINTERS POINTER is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address

3. Senem KUMOVA METN CS115 2008-2009 FALL 3 POINTERS int x= 5 is a variable and 5 is the value of it printf(value of x : %d, x); &x is the location or address of the x printf(address of x in memory : %d, &x); Pointers are declared in programs and then used to take addresses !!! int x=5; // some stored value int * p; // a pointer that can hold the address of an integer p=&x; x= 5 &x . . .. MEMORY

4. Senem KUMOVA METN CS115 2008-2009 FALL 4 Pointers: & operator 5 5 0x12345678 0x12345678 x x int x = 5 ; int x = 5 ; p p int *p ; int *p ; p = & x ; p = & x ; // adress of x is // stored in p // adress of x is // stored in p 0x12345678 0x12345678 p is a pointer: it contains the address of a piece of data p is a pointer: it contains the address of a piece of data Memory Address Memory Address &x &x

5. Senem KUMOVA METN CS115 2008-2009 FALL 5 POINTERS int * p; // A pointer that can hold an address // of an integer integer pointer int a =0; p= &a; double * q // A pointer that can hold an address // of a double double pointer double b=1.2; q= &b; char * t // A char pointer char c=D; t = &c;

6. Senem KUMOVA METN CS115 2008-2009 FALL 6 POINTERS main() { int * p; // integer pointer int a =0; p= &a; // a equals to *p printf(%d \n, a); printf(%d \n, *p); *p=6; printf(%d \n, a); printf(%d \n, *p); a++; printf(%d \n, a); printf(%d \n, *p); } a=0 p=&a *p

7. Senem KUMOVA METN CS115 2008-2009 FALL 7 POINTERS int a=1, b=2, *p; p=&a; &a &b &p a=1 b=2 ?? will point somewhere in memory &p 1 2 p=&a &a &b

8. Senem KUMOVA METN CS115 2008-2009 FALL 8 POINTERS b=*p; // equivalent to b=a &a &b &p a=1 b=1 ( the value of a ) p=&a THE VALUE POINTED BY A POINTER CAN BE CHANGED BY * name_pointer *p

9. Senem KUMOVA METN CS115 2008-2009 FALL 9 Pointers : EXAMPLE 1 #include void main(void) { int i = 7, j=3 , * k, *m; k = &i; // k ??? *k ??? j=*k; // k ??? *k ??? m=&i; // i ??? *i ??? (*m) ++; // m ??? *m ??? printf( %d %d %d %d, i, j, *k, *m); }

10. Senem KUMOVA METN CS115 2008-2009 FALL 10 Pointers : EXAMPLE 2 #include void main(void) { char a = D; char *p = &a; *p=*p+2; printf(%c %c, a, *p); a=a+1; printf(%c %c, a, *p); }

11. Senem KUMOVA METN CS115 2008-2009 FALL 11 CALL BY VALUE /* Whenever variables are passed as arguments to a function, their values are copied to the function parameters */ void main() { int a=20; incr1 (a); printf(%d: , a); incr2 (a); printf(%d: , a);} void incr1(int x) { x++; } void incr2(int a) { a++; } R E M E M B E R !

12. Senem KUMOVA METN CS115 2008-2009 FALL 12 CALL BY REFERENCE /* Pointers are passed as arguments to a function, their addresses are assigned to the function parameters defined as pointers:*/ void main() { int a=20; incr (&a); printf(%d: , a); } void incr(int * x) { (*x)++; }

13. Senem KUMOVA METN CS115 2008-2009 FALL 13 CALL BY REFERENCE : Example 1 void addi(int *a, int b); void main() { int a=20, int b=30; addi (&a , b) printf(%d %d :, a, b);} void addi(int *a, int b) { int tmp = *a +b; *a= tmp; b= b++;} OUTPUT??

14. Senem KUMOVA METN CS115 2008-2009 FALL 14 void main() { int a=20; int b=30; swap1 (a, b); swap2 (&a, &b); printf(%d %d: , a, b);} void swap1(int x, int y) { int tmp; tmp=x; x=y; y=tmp; } void swap2(int *x, int *y) { int tmp; tmp = *x; // get value pointed by x. *x = *y; // assign value pointed by y to x *y = tmp; } CALL BY REFERENCE Example 2 : SWAP

15. Senem KUMOVA METN CS115 2008-2009 FALL 15 What is an Array? int grade0, grade1, grade2; int grade [3]; Arrays make it possible to represent large number of homogenous values grade0 grade1 grade2 grade[0] grade[1] grade[2]

16. Senem KUMOVA METN CS115 2008-2009 FALL 16 One Dimensional Arrays type name_of_array[size] EXAMPLE int grade[5]; data data grade[1] data grade[2] data data grade[0] grade[3] grade[4] MEMORY first element in array fift ( the last) element in array starts from 0 ends at size - 1

17. Senem KUMOVA METN CS115 2008-2009 FALL 17 One Dimensional Arrays int grade[5], x=9, y; grade[0]= 0; grade[3]= 4; grade[2]= -1; grade[1]=grade[3]+grade[2]; grade[4]= x; x= grade[2]; y= grade[grade[3]]; 0 3 grade[1] -1 grade[2] 4 9 grade[0] grade[3] grade[4] MEMORY x ? y ?

18. Senem KUMOVA METN CS115 2008-2009 FALL 18 INITIALIZATION float f1[]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration without the size */ float f2[4]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration with size */ char z[] =abc; // char arrays char z[] = {a, b, c, \0} int a[80]={0};

19. Senem KUMOVA METN CS115 2008-2009 FALL 19 EXAMPLE1: /* array1.c*/ main() { int x[10], k; for(k=0; k<10; k++) { x[k]=0; printf(x[%d] = %d\n, k, x[k]); //what will be the output ??? } }

20. Senem KUMOVA METN CS115 2008-2009 FALL 20 EXAMPLE2: /* array2.c*/ main() { int x[10], k; for(k=0; k<10; k++) { x[k]=k; printf(x[%d] = %d\n, k, x[k]); //what will be the output ??? } }

21. Senem KUMOVA METN CS115 2008-2009 FALL 21 EXAMPLE3: /* array3.c*/ main() { int x[10], k, sum=0; for(k=0; k<10; k++) scanf(%d, &x[k]); for(k=0; k<10; k++) { printf(%d, x[k]); sum=sum + x[k]; } printf(sum is %d, sum); }

22. Senem KUMOVA METN CS115 2008-2009 FALL 22 Relationship between Pointers and Arrays /*An array name by itself is an adress or a pointer value */ int a[5] , *p; p=&a[0]; // Equals to p=a;

23. Senem KUMOVA METN CS115 2008-2009 FALL 23 Relationship between Pointers and Arrays &a[0] equals to a then a[0] equals to *a &a[1] equals to a+1 then a[1] equals to *(a+1) &a[2] equals to a+2 then a[2] equals to *(a+2) &a[i] equals to a+i then a[i] equals to *(a+i) EXAMPLE: int a [5]={1,2,3,4,5}; int * p; printf(%d,a[0]); printf(%d,*a); printf(%d,a[2]); printf(%d,*(a+2)); p=&a[4]; P=a+4;

24. Senem KUMOVA METN CS115 2008-2009 FALL 24 Relationship between Pointers and Arrays : Example1 main() { int x[10], k; for(k=0; k<10; k++) { x[k]=k; printf(%d\n, x[k]); } } main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=k; printf(%d\n, *(x+k)); } }

25. Senem KUMOVA METN CS115 2008-2009 FALL 25 Relationship between Pointers and Arrays : Example2 main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=0; printf(%d\n, *x+k); } } main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=0; printf(%d\n, *(x+k)); } }

26. Senem KUMOVA METN CS115 2008-2009 FALL 26 Pointer Arithmetic EXAMPLE double a[2],*p,*q; p=a; // p=&a[0] q=p+1; // q=&a[0]+1= &a[1] printf(%d\n, q-p); /* difference in terms of array elements */ printf(%d\n, (int) q-(int) p); /* difference in memory locations */

27. Senem KUMOVA METN CS115 2008-2009 FALL 27 Arrays as Function Arguments main() { int x[9], r; r=sum(x,9); //sum(&x[0],9) } double sum( int a[], int n) { int i; double result =0.0; for (i=0; i

28. Senem KUMOVA METN CS115 2008-2009 FALL 28 Dynamic Memory Allocation Calloc : contiguous memory allocation Malloc : memory allocation

29. Senem KUMOVA METN CS115 2008-2009 FALL 29 CALLOC calloc(n, el_size) an array of n elements, each element having el_size bytes int main() { int *a; //will be used as an array int n; // size of array .... a=calloc(n, sizeof(int)); /* get space for a , and initialize each bit to zero */ free(a); /* each space opened dynamically must be returned */ }

30. Senem KUMOVA METN CS115 2008-2009 FALL 30 MALLOC /* malloc does not initialize memory */ int main() { int *a; //will be used as an array int n; // size of array .... a=malloc(n*sizeof(int)); /* get space for a */ .... free(a); }

31. Senem KUMOVA METN CS115 2008-2009 FALL 31 DYNAMIC ARRAY : Example main() { int * x, k,s; printf(give array size); scanf(%d,&s); x=malloc(s*sizeof(int)); for(k=0; k

32. Senem KUMOVA METN CS115 2008-2009 FALL 32 NEXT WEEK MULTIDIMENSIONAL ARRAYS SORTING ALGORITHMS : BUBBLE SORT MERGE SORT STRINGS && STRING_HANDLING FUNCTIONS ARRAYS OF POINTERS ARGUMENTS TO MAIN