Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.

Similar presentations


Presentation on theme: "Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh."— Presentation transcript:

1 Chapter 5 Pointers and Arrays Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangFunctions and Program Structure Pointers and Arrays A pointer is a variable that contains the address of a variable. Pointers and arrays are closely related Pointers and arrays are closely related

3 3Ku-Yaw ChangFunctions and Program Structure 5.1 Pointers and Address A simplified picture of how memory is organized An array of consecutively numbered or addressed memory cells An array of consecutively numbered or addressed memory cells Any byte can be a char Any byte can be a char A pair of one-byte cells can be a short integer A pair of one-byte cells can be a short integer Four adjacent bytes form a long Four adjacent bytes form a long A pointer is a group of cells (usually two or four) that hold an address A pointer is a group of cells (usually two or four) that hold an address Check sizeof(char *), sizeof(int *), sizeof(void *)

4 4Ku-Yaw ChangFunctions and Program Structure 5.1 Pointers and Address c is a char and p is a pointer that points to it The unary operator & gives the address of an object p = &c; p = &c; P: C:

5 5Ku-Yaw ChangFunctions and Program Structure 5.1 Pointers and Address The unary operator * is the indirection or dereference operator ip = *x; ip = *x;Example int x = 1, y = 2, z[10]; int * ip; ip = &x y = *ip; *ip = 0; ip = &z[0]

6 6Ku-Yaw ChangFunctions and Program Structure 5.2 Pointers and Function Arguments C pass arguments to functions by value No direct way for the called function to alter a variable in the calling function No direct way for the called function to alter a variable in the calling function Example Example swap(a, b); void swap(int x, int y) /* wrong */ { int temp; temp = x; x = y; y = temp; }

7 7Ku-Yaw ChangFunctions and Program Structure 5.2 Pointers and Function Arguments Example swap(&a, &b); void swap(int * px, int * py) { int temp; temp = *px; *px = *py; *py = temp; } in caller: a: b: px : py : in swap:

8 8Ku-Yaw ChangFunctions and Program Structure 5.3 Pointers and Arrays A strong relationship between pointers and arrays Any operation that can be achieved by array subscripting can also be done with pointers. Any operation that can be achieved by array subscripting can also be done with pointers.

9 9Ku-Yaw ChangFunctions and Program Structure 5.3 Pointers and Arrays int a[10]; Define an array a of size 10 A block of 10 consecutive objects: a[0], a[1], …, a[9] A block of 10 consecutive objects: a[0], a[1], …, a[9] a[i] refers to the i-th element of the array a[i] refers to the i-th element of the array int * pa; pa = &a[0]; (also written pa = a) pa contains the address of a[0] pa contains the address of a[0] x = *pa; x = *pa; a[0] a: pa:

10 10Ku-Yaw ChangFunctions and Program Structure 5.3 Pointers and Arrays strlen(“hello, world”); string constant string constantstrlen(array); char array[100]; char array[100];strlen(ptr); char * ptr; char * ptr; /* strlen: return length of string s */ int strlen(char * s) { int n; for (n=0; *s != ‘\0’; s++) n++; return n; } An array name is passed to a function The location of the initial element The location of the initial element Parameters in a function definition char s[] and char * s are equivalent char s[] and char * s are equivalent

11 11Ku-Yaw ChangFunctions and Program Structure 5.4 Address Arithmetic pa points to a particular element of an array pa + i : i elements after pa pa + i : i elements after pa pa - i : i elements before pa pa - i : i elements before pa pa points to a[0] *(pa+1) refers to the contents of a[1] *(pa+1) refers to the contents of a[1] pa + i is the address of a[i] pa + i is the address of a[i] *(pa + i) is the contents of a[i] *(pa + i) is the contents of a[i] a[0] a: pa: pa+1:pa+2:

12 12Ku-Yaw ChangFunctions and Program Structure 5.4 Address Arithmetic In evaluating a[i], C converts it to *(a+i) immediately One difference A pointer is a variable A pointer is a variable pa = a and pa++ are legal pa = a and pa++ are legal An array name is not a variable An array name is not a variable a = pa and a++ are illegal a = pa and a++ are illegal

13 13Ku-Yaw ChangFunctions and Program Structure 5.5 Character Pointers and Functions A string constant is an array of characters “I am a string” “I am a string” Terminated with the null character ‘\0’ The length in storage is thus one more than the number of characters between the double quotes An important difference between char amessage[] = “now is the time”; /* an array */ char * pmessage = “now is the time”; /* a pointer to a string constant*/ amessage is an array amessage is an array always refer to the same storage pmessage is a pointer pmessage is a pointer May be modified to point elsewhere String contents cannot be modified

14 14Ku-Yaw ChangFunctions and Program Structure 5.5 Character Pointers and Functions /* strcpy: copy t to s; array subscript version */ void strcpy(char *s, char *t) { int i; i = 0; while ((s[i] = t[i]) != ‘\0’) i++;}

15 15Ku-Yaw ChangFunctions and Program Structure 5.5 Character Pointers and Functions /* strcpy: copy t to s; pointer version 1*/ void strcpy(char *s, char *t) { while ((*s = *t) != ‘\0’) { s++;t++;}}

16 16Ku-Yaw ChangFunctions and Program Structure 5.5 Character Pointers and Functions /* strcpy: copy t to s; pointer version 2*/ void strcpy(char *s, char *t) { while ((*s++ = *t++) != ‘\0’) ;}

17 17Ku-Yaw ChangFunctions and Program Structure 5.5 Character Pointers and Functions /* strcpy: copy t to s; pointer version 2*/ void strcpy(char *s, char *t) { while ( *s++ = *t++ ) ;}

18 18Ku-Yaw ChangFunctions and Program Structure 5.6 Pointer Arrays; Pointers to Pointers Pointers are variables themselves Can be stored in arrays just as other variables can Can be stored in arrays just as other variables canExample #define MAXLINES 5000 char * lineptr[MAXLINES]; lineptr[0] lineptr[1] lineptr[2] ‘\0’

19 19Ku-Yaw ChangFunctions and Program Structure 5.6 Pointer Arrays; Pointers to Pointers int a[30]; int * pa; pa = (int *) malloc(30*sizeof(int)); free(pa); Dynamic Memory Allocation Obtain memory space at execution time Obtain memory space at execution time Allocate memory blocks void *malloc( size_t size ); void *malloc( size_t size ); Deallocates or frees a memory block void free( void *memblock ); void free( void *memblock );

20 20Ku-Yaw ChangFunctions and Program Structure 5.10 Command-line Arguments Pass command-line arguments or parameters to a program when it begins executing

21 21Ku-Yaw ChangFunctions and Program Structure 5.10 Command-line Arguments int argc: argument count The number of command-line arguments The number of command-line arguments char * argv[]: argument vector A pointer to an array of character A pointer to an array of characterExample echo hello, world echo hello, world argc : 3 argv[0] : “echo” argv[1] : “hello,” argv[2] : “world”

22 22Ku-Yaw ChangFunctions and Program Structure 5.10 Command-line Arguments echo main(int argc, char * argv[]) { int i; for ( i = 1; i < argc; i++ ) printf(“%s%s”, argv[i], (i<argc-1)?” “:””); printf(“\n”); return 0; } main(int argc, char * argv[]) { int i; for ( i = 1; i < argc; i++ ) printf(“%s%s”, argv[i], (i<argc-1)?” “:””); printf(“\n”); return 0; }

23 To be continued…


Download ppt "Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh."

Similar presentations


Ads by Google