1 / 21

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

Chapter 5 : Pointers and Arrays Present by : Le Thi Hien. Contents. Pointer and Addresses Pointer and F unction Arguments Pointer and 1-dimensional Arrays Address Arithmetic Pointer and Multi-dimensional A rrays Pointer to Pointer Pointer Arrays Function Pointers.

louisw
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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 !

More Related