Download
chapter 5 pointers and arrays present by le thi hien n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 5 : Pointers and Arrays Present by : Le Thi Hien PowerPoint Presentation
Download Presentation
Chapter 5 : Pointers and Arrays Present by : Le Thi Hien

Chapter 5 : Pointers and Arrays Present by : Le Thi Hien

95 Vues Download Presentation
Télécharger la présentation

Chapter 5 : Pointers and Arrays Present by : Le Thi Hien

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 5 : Pointers and Arrays Present by : Le ThiHien

  2. Contents • Pointer and Addresses • Pointer and Function Arguments • Pointer and 1-dimensional Arrays • Address Arithmetic • Pointer and Multi-dimensional Arrays • Pointer to Pointer • Pointer Arrays • Function Pointers

  3. 1. Pointer and Adresses • A pointer is a variable which contains the address of a variable. • Form : data_type *pointer_name; • Eg : int *p, *q, a;//a is variable int x = 2; //p, q is pointer. p = &x; float y; p= &y;//error p = &(int) y;

  4. 1. Pointer and Adresses • int *p, *q;

  5. 2. Pointer and Function Arguments Result :

  6. 3. Pointer and 1-dimensional Arrays • int a[10]; int *p; p = a; or : p = &a[0]; • scanf ( " %d ", &a[i]); ( 1)scanf ( " %d ",a + i); ( 2)scanf ( " %d", p + i ); ( 3) scanf ( " %d", &p[i] ); ( 4) (1), (2), (3), (4) is the same effect. Result : ?

  7. 3. Pointer and 1-dimensional arrays a pa

  8. 3. Pointer and 1-dimensional arrays If use : amess = “run out of time”

  9. 3. Pointer and 1-dimensional arrays • 4 functions have the same effect : 1. 2. 3. 4.

  10. 4. Address Arithmetic • The goal of using pointer ??? • Increase the processing speed • Save memory • Make affect to data when use function. • To allocates a block of memory : void *malloc(size_t size); void *calloc(size_t n, size_t size); • To reallocates : void realloc(void *pointer, size_t size); • To free : void free (void *ptr);

  11. 4. Address Arithmetic

  12. 4. Address Arithmetic • Eg : • int *a = (int*)malloc(10 *size(int)); /*declarate and allocates an array contains 10 int elements*/ • int *b = (int*)calloc(20, sizeof(int)); /*delarate and allocates an array contains 20 int elements*/ • a = realloc(a, 20*sizeof(int)); /*extend a array more 10 elements.*/ • free(a);/*need free memory*/ • free(b);

  13. 5. Pointer and Multi-dimensional arrays • Form : data_typea_name[][]..[]; • Eg : • Using array with index:

  14. 5. Pointer and Multi-dimensional arrays - Using pointer :

  15. 5. Pointer and Multi-dimensional arrays

  16. 6. Pointer Arrays • Form : data_type *ptr_name[elements_number]; • Compare : int a[2][3]; And int *b[2]; a[0] a[1] a b

  17. 7. Pointer to pointer • Eg : int a = 3; int *b = &a; int **c = &b; int ***d = &c; *d = c **d = *c = b ***d = **c = *b =a Lineptr[MAXLINES]

  18. 7. Pointer to pointer • Eg : #include<…> #define MAXLINE 5000 char *lineptr[MAXLINE]; intread_lines(char *lineptr[], intnlines) { char *p, line[MAXLEN]; … line[nlines++] = p; … } void write_lines(char *lineptr[], intnlines); void swap (char *v[], int I, int j); • void q_sort(char *lineptr[], int left, int right) • { • … • void swap(char *v[], int i, int j); • … • swap(lineptr, ++last, i); • … • } int main() { … read_lines(lineptr, MAXLINES); q_sort(lineptr, 0, nlines); write_lines(linptr, nlines); } Pointer Arrays Pointer to pointer

  19. Function Pointer and Pointer Function • In C, function name ís the address of that function. • Can assigment a pointer to function name. • Form : • data_type (*func_name)(argument _list); /*pointer to function*/ • data_type *func_name(argument_list); /*function which return a pointer*/ • Goal : Charge a function as a argument of other function

  20. Function Pointer and Pointer Function • Eg : … void qsort(void *lineptr[], int left, int right, int (*comp) (void *, void *)); intnumcmp(char *, char *); … intmain() { … qsort((void **) lineptr, 0, nlines– 1, (int (*) (void*, void*)(numeric ? numcmp : strcmp)) ); … }

  21. Thank you !