Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.

Similar presentations


Presentation on theme: "0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers."— Presentation transcript:

1 0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers 5.7Multidimensional Arrays 5.8Initialization of Pointer Arrays 5.9Pointers vs Multi-dimensional Arrays 5.10Command-line Argument 5.11Pointers to Functions 5.12Complicated Declarations Session 7 - 1 May 2007 Session 8 - 8 May 2007 Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip !!! 15 May 2007 : Mid Term Test !!!

2 1 5.1 Pointers and Addresses Pointer – A pointer is a variable that contains the address of a variable Address Operator & –&x is the address of the variable x Indirection or Dereferencing Operator * –*p access the object the pointer p points to A first example main() { int x = 1; int *p; p = &x; printf(“%d”, *p); } … … … FF00p&p F000&xx1*p F000 0000 FFFF variables addresses name value Full Memory Diagram (with name and value for the variables and addresses) p is a pointer to a variable of type int

3 2 #include main() { int i = 1; int *p; int **q; p = &i; q = &p; A Mystery Program Format TypeConverted to dintsigned decimal notation x, Xint unsigned hexadecimal notation (without the leading 0x or 0X) p* voidPrint as a pointer (implementation- dependent representation) Reminder printf(" i = %d p = %X q = %x\r\n", i, p, q ); printf(" *p = %d &i = %X &p = %x\r\n", *p, &i, &p ); printf("**q = %d *q = %X &q = %x\r\n", **q, *q, &q ); printf("\n"); printf("*&*&*&*&i = %d &*&*&*&i = %p\r\n", *&*&*&*&i, &*&*&*&i ); printf("*&*&*&*&*p = %d &*&*&*&*p = %p\r\n", *&*&*&*&*p, &*&*&*&*p ); printf("\nmystery: %d %p %p %p", p[0], &p[0], &p[1], &p[2]); p += 3; printf(" %p\r\n", p); } q p i1

4 3 i = 1 p = BFFFFB08 q = bffffb0c *p = 1 &i = BFFFFB08 &p = bffffb0c **q = 1 *q = BFFFFB08 &q = bffffb10 *&*&*&*&i = 1 &*&*&*&i = 0xbffffb08 *&*&*&*&*p = 1 &*&*&*&*p = 0xbffffb08 mystery: 1 0xbffffb08 0xbffffb0c 0xbffffb10 0xbffffb14 Output of the Mystery Program Reminder int i=1; int *p=&i; int **q=&p; Full Memory Diagram 1iBFFFFB08 &i q p i1 BFFFFB08pBFFFFB0C &p &*p*p BFFFFB0Cq BFFFFB10 &q *q&*q **q&**q Memory Diagram

5 4 5.2 Pointers and Function Arguments C passes arguments to functions by value ! void swap1(int x, int y) {/* WRONG */ int temp; temp = x; x = y; y = temp; } void swap2(int *px, int *py) {/* interchange *px and *py */ int temp; temp = *px; *px = *py; *py = temp; } The values of a and b have been swapped !!! main() swap2(&a,&b) swap2(int *px1, int *py1) px1 = &a; py1 = &b; … As x1 and y1 are local variables, only the copies of a and b have been swapped !!! main() swap1(a,b) swap1(int x1, int y1) x1 = a; y1 = b; … py1 b a px1

6 5 5.3 Pointers and Arrays, 5.4 Address Arithmetic a The name of this array is “a” and its value is equivalent to the address of its first element: &a[0]. a+2 p+3 a+i points to the i-th element beyond a. p+i points to the i-th element beyond p. As p is a variable, p=a or ++p are legal; but as an array name is not a variable, expression like a=p or ++a are illegal ! p int *p=a; a[3] a[1] a[0] a[2] int a[4]; The declaration int a[4]; defines an array of size 4, that is, a block of 4 consecutive objects of type int, named a[0], a[1], a[2], a[3]. Tricky: p-a is legal, but not p+a !

7 6 5.5 Character Pointers and Functions The following calls will work: // return length of string s int strlen(char s[]) { int n; for (n = 0; *s != '\0'; s++) n++; return n; } That means, as a formal parameter in a function definition, char s[] and char *s are equivalent. Within the called function, this array name is a local pointer variable! When an array name is passed to a function, what is passed is the location of the first element. strlen("hello world"); // string constant !! strlen(a); // char a[10]; strlen(p); // char *p;

8 7 A Test Program #include /* Return length of string s */ int strlen(char s[]) { int n; for (n = 0; *s != '\0'; s++) n++; return n; } main() { int i1, i2, i3; char a[] = "hello"; char *p = a; (1) i1 = strlen(a); (2) i2 = strlen(p); (3) i3 = strlen("hello"); } '\0' 'o' 'l' 'e' 'h' p a '\0' 'o' 'l' 'e' 'h' strlen.s (3) '\0' 'o' 'l' 'e' 'h' p a strlen.s (1) and (2) Hint: Draw the function call and execution stack diagrams for (1), (2) and (3)


Download ppt "0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers."

Similar presentations


Ads by Google