chapter 5 : pointers and arrays present by : le thi hien
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Chapter 5 : Pointers and ArraysPresent by : Le Thi Hien
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
2/21
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;
p = &x = 0xbee17a4
q = p = 0xbee17a4
x = *p = 2
3/21
1. Pointer and Adresses
int *p, *q;
True False
p +2; p - 2;p –q;
p +q;p * q;p *2; p /2;p /q;p%q;
Syntax Result
++*p *p = (*p) + 1 before execute
*++p p = p+1 get (*p) before execute
*p++ = *(p++) Get (*p) execute p = p+1
(*p)++ Get (*p) execute *p =*p +1
4/21
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 : ?
6/21
3. Pointer and 1-dimensional arrays
Pointer Array’s name
- Is a variable contains the adress of data which is pointed.
- Need supply memory to save pointer.
- p++, ++p, p +=1 is legal.
- Eg :void copy(char*a, char*b);char a[] = “hello”;char b[]=“world”;char *pa =a;char *pb =b;copy(pa, pb);/*copy b[] to a[]*/
- Is a constant contains the address of the first element of the array.
- No need memory for array’s name value.
- Can’t a++, ++a, a +=1.- Eg :
char a[]=“hello”;char b[]=“world”;copy(a,b);/*copy b to a*/
pa a[0] a[1] a[2] a[3]
a
7/21
3. Pointer and 1-dimensional arrays
If use :amess = “run out of time”
8/21
3. Pointer and 1-dimensional arrays
4 functions have the same effect :
1. 2.
3. 4.
9/21
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);
10/21
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);
12/21
5. Pointer and Multi-dimensional arrays
Form : data_type a_name[][]..[]; Eg :
Using array with index:
13/21
5. Pointer and Multi-dimensional arrays
- Using pointer :
14/21
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
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
b[0] b[1]
c[0][0] c[0][1] d
16/21
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
line(nlines++ ) = p;
P = alloc(len)
A B C \0D E \0
F G H \0
Lineptr[MAXLINES]
17/21
7. Pointer to pointer
Eg :#include<…>#define MAXLINE 5000
char *lineptr[MAXLINE];
int read_lines(char *lineptr[], int nlines){ char *p, line[MAXLEN]; … line[nlines++] = p; …}void write_lines(char *lineptr[], int nlines);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 to pointer
Pointer Arrays
18/21
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
19/21
Function Pointer and Pointer Function
Eg :…void qsort(void *lineptr[], int left, int right, int (*comp) (void *, void *));int numcmp(char *, char *);…int main(){…qsort((void **) lineptr, 0, nlines – 1,(int (*) (void*, void*)(numeric ? numcmp : strcmp)) );…}
20/21