Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer science 2009-2010 C programming language lesson 3.

Similar presentations


Presentation on theme: "Computer science 2009-2010 C programming language lesson 3."— Presentation transcript:

1 Computer science 2009-2010 C programming language lesson 3

2 Aims of lesson 3 Pointers

3 Introduction In the computer memory, data are stored in a binary form. B01C … B01F Memory is divided into 8 bit cells called bytes. Each cell has its own address, an address being an hexadecimal number.

4 Introduction This statement implies two operations : float a; - Definition of a variable that can be used in the program to manipulate data. - Allocation of a memory space where the value of the variable will be stored. a B01C … B01F

5 Introduction Arguments are passed to functions by value : float a,b; a=5;b=a; fonc(a); Function fonc works with a copy of the variable a A function can not alter the value of a variable that is passed as argument : main() { float a; a=5; … fonc(a); printf(«%f\n»,a);} fonc(float a) {… a=0; … } « 5 » is displayed ! A function works with a copy of the variables. The variables inside the function are stored at another address. a adra 5 x adrx 5

6 The pointers Solution : - Pass the address of the variable instead of its value A pointer is a variable that contains the address of another variable. To access the address of an already existing data : & operator float n; Declaration of a pointer : - To manipulate this address, it is necessary to define a variable that contains it : what is its type ? float *p; p is a « pointer to a float » or *p is a float p is the address of the first memory cell that contains the data &n : represents the address of the variable n To access the value of the variable pointed by p : * operator (indirection or deferencing operator) *p represents the contents of the variable pointed by p

7 Example int n; int *p; … n=5; printf(«%d\n »,n); p=&n; printf(«%d\n »,*p); *p=1; printf(«%d\n »,n); printf(«%d\n »,*p); printf(«%x %x\n »,p,&n); printf(«%x %x\n »,p+1,&n+2 ); 5 5 *p is the contents of the memory cell pointed by p 1 1 BC01 BC01 (address in hexa) BC05 BC09

8 main( ) { double n; double *p; n=5; printf(«%lf\n»,n); p=&n; printf(«%lf\n»,*p); *p=1; printf(«%lf\n»,n); printf(«%lf\n»,*p); printf(«%x %x\n»,p,&n); printf(«%x %x\n»,p+1,&n+2 );}

9 Arrays and pointers Another example : declaration of an array - Definition of a variable that can be used in the program to manipulate data. - Allocation of a memory space of 100 adjacent cells of 4 bytes each int tab[100]; In fact an array is nothing else than a pointer ! (roughly) int tab[100]; for(i=0;i<n;i++) tab[i]=i; printf(«%d\n »,tab[0]); printf(«%d\n »,*tab); printf(«%d %d\n »,*(tab+1),tab[1]); printf(«%x %x\n»,tab,tab+5); the variable tab is a pointer that contains the address of the first element of the array 0 0 1 AB08 AB0C

10 Advantage of arrays : no need to care about the memory allocation. Disadvantage : the size of the array should be known at the time it is declared (static allocation) Consequence : the size of the array has to be known at compile time. int n; int tab[n]; … n=100; for(i=1;i<n;i++) tab[i]=0; Arrays and pointers Another example : declaration of an array - Definition of a variable that can be used in the program to manipulate data - Allocation of a memory space of 100 adjacent cells of 4 bytes each int tab[100];

11 Dynamic allocation Solution : - Declare a variable containing an address i.e. a pointer - Dynamically allocate the necessary memory space at this address #include... int dim; int *tab;... dim = 100; tab=malloc(dim*sizeof(int)); for(i=0;i<dim;i++) tab[i]=0; - Memory allocation function : malloc(# bytes) - sizeof(type) : returns the size of a variable in # bytes

12 #include... int dim; int *tab;... dim=100; tab=malloc(dim*sizeof(int)); for(i=0;i<n;i++) tab[i]=0; Dynamic allocation Advantage : the dimension of an array does not need to be known to declare the variable that points to the array. - You have to free the memory space after use : The function is : But : before using an array you absolutely have to allocate its memory space ! … free(tab); free (pointer) But in general the program crashes here ! No problem at compile time

13 #include... int dim; int *tab;... dim=100; tab=malloc(dim*sizeof(int)); for(i=0;i<n;i++) tab[i]=0; Dynamic allocation Advantage : the dimension of an array does not need to be known to declare the variable that uses the array. - You have to free the memory space after use : The function is : But : before using an array you absolutely have to allocate its memory space ! … free(tab); free (pointer)

14 Pointer arithmetic - The operator + allows to move forward in the computer memory from the address contained in the pointer. If p points to a data of size T then p+5 points on the memory position that is 5T positions beyond int *tab; tab=malloc(100*sizeof(int)); for (i=0;i<100;i++) *(tab+i)=3*i; printf(«%d,%d,%d\n»,*tab,*(tab+1),*(tab+99)); Another possible writing printf(«%d,%d,%d\n»,tab[0],tab[1],tab[99]);

15 -The operator – allows to move backward. - The operators ++ et – allow to increment and decrement a pointer. NULL Pointer : by convention, it points to address #0000 float *p; p = NULL; Example : the malloc function returns a pointer on the allocated memory space, if the allocation is successful the NULL pointer if the allocation is unsuccessful tab=malloc(dim*sizeof(int)); if (tab!=NULL) for(i=0;i<dim;i++) tab[i]=0; else printf(« Allocation impossible\n »); Pointer arithmetic


Download ppt "Computer science 2009-2010 C programming language lesson 3."

Similar presentations


Ads by Google