220 likes | 350 Views
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.
E N D
Chapter 5 : Pointers and Arrays Present by : Le ThiHien
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
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;
1. Pointer and Adresses • int *p, *q;
2. Pointer and Function Arguments Result :
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 : ?
3. Pointer and 1-dimensional arrays If use : amess = “run out of time”
3. Pointer and 1-dimensional arrays • 4 functions have the same effect : 1. 2. 3. 4.
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);
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);
5. Pointer and Multi-dimensional arrays • Form : data_typea_name[][]..[]; • Eg : • Using array with index:
5. Pointer and Multi-dimensional arrays - Using pointer :
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
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]
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
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
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)) ); … }