Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.

Similar presentations


Presentation on theme: "CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a."— Presentation transcript:

1 CSC 215 Pointers and Arrays

2 Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a variable, results in the address of the variable. This is the address operator The operator *, when applied to a pointer, returns the value stored at the address specified by the pointer. This is the dereferencing or indirection operator

3 Variable Allocate 1 byte of memory char a; Memory allocated at some particular address char *ptr; ptr = &a; All pointers are of the same size they hold the address generally 4 bytes

4 More Pointers int x = 1, y = 2, z[10]; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ ip = &z[0]; /* ip now points to z[0] */

5 What is the output? int x = 1, y = 2, z[10]; int *ip; ip = &x; *ip = *ip + 1; printf(“%d %d %d”, x, y, *ip); y = *ip + 1; printf(“%d %d %d”, x, y, *ip); *ip += 1; printf(“%d %d %d”, x, y, *ip);

6 What is the output? int x = 1, y = 2, z[10]; int *ip; ip = &x; *ip = *ip + 1; printf(“%d %d %d”, x, y, *ip); /* 2 2 2 */ y = *ip + 1; printf(“%d %d %d”, x, y, *ip); /* 2 3 2 */ *ip += 1; printf(“%d %d %d”, x, y, *ip); /* 3 3 3 */

7 Pointers and Function Arguments C passes arguments to functions by value. The called function can’t alter a variable in the calling function. The effect is applied on copy of the passed argument. To obtain the desired effect on the actual argument, pass pointers to the argument to be changed.

8 Swap two numbers void swap (int x, int y) /*wrong*/ { int temp; temp = x; x = y; y = temp; } main() { int a = 5, b = 3; swap (a, b); printf (“After swap: %d %d”, a, b); }

9 Swap two numbers void swap (int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } main() { int a = 5, b = 3; swap (&a, &b); printf (“After swap: %d %d”, a, b); }

10 Arrays and Pointers #include main() { int a[4] = {11, 12, 13, 14}; int x, y; int *pa; pa = &a[0]; x = *pa; y = *(pa + 2); printf ("%d %d %d %d", *pa, x, y, a[2]); /* 11, 11, 13, 13 */ }

11 Arrays and Pointers p = a; is equivalent to p = &a[0]; p++; is equivalent to p+=1; is equivalent to p=&a[1]; x = a[i]; is equivalent to x= *(a + i); If p is a pointer to some element of an array a: p++ increments p to point to the next element. p+= i increments it to point i elements beyond where it currently does. Reference to a[i] can also be written as * (a+i). A pointer is a variable, so p=a and p++ are legal. But an array name is not a variable; so a=p and a++ are illegal

12 Passing Array to function When an array name is passed to a function, what is passed is the location of the initial element. Within the called function, the received location (address) can be treated as a pointer. It is possible to pass part of an array to a function, by passing a pointer to the beginning of the subarray. For example, if a is an array, function1(&a[2])

13 Arrays and Pointers #include int mystrlen (char s[]) { int i, len = 0; for (i = 0; s[i] != '\0'; i++) len++; return len; } main() { char str[10]; strcpy(str, “CSC215”); printf(“%d”, mystrlen(str)); }

14 Arrays and Pointers #include int mystrlen (char *s) { int i, len = 0; for (; *s != '\0'; s++) len++; return len; } main() { char str[10]; scanf(“%s”,str); printf(“%d”, mystrlen(str)); printf(“%d”, mystrlen(&str[2])); }

15 Array of pointers #include int main() { int x = 11, y = 12, z = 13, i; int *a[3]; /*array of pointers*/ a[0] = &x; a[1] = &y; a[2] = &z; for (i = 0; i < 3; i++) printf("%d\n", *a[i]); }

16 Pointers to Functions int sum(int a, int b) { return a+b; } main() { int c; int (*fp)(int, int); fp = sum; c = (*fp)(5, 6); /* c = sum (5, 6); */ printf(“%d\n”,c); }

17 Pointer Arithmetic Arithmetic operators “+”, “-”, “++”and “--”can be applied to pointers. The result depends on the data type of the pointer. The result is undefined if the pointers do not point to the elements within the same array

18 Example #include int main() { char s[100] = "riyadh"; char *p1 = &s[2]; printf(" %c", *p1); // y printf(" %c", *++p1); // a char *p2 = &s[0]; printf(" %d", p2 - p1); // -3 printf(" %d", p1 - p2); // 3 return 0; }

19 Pointer arithmetic #include const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have array address in pointer */ ptr = var; for ( i = 0; i < MAX; i++) { printf("Address of var[%d] = %x\n", i, ptr ); printf("Value of var[%d] = %d\n", i, *ptr ); /* move to the next location */ ptr++; } return 0; }

20 Pointer arithmetic The output is something as follow: Address of var[0] = bf882b30 Value of var[0] = 10 Address of var[1] = bf882b34 Value of var[1] = 100 Address of var[2] = bf882b38 Value of var[2] = 200

21 Pointer arithmetic The unary operators & and * have the same precedence as any other unary operator, with associativity from right to left. c=*++cp is equivalent to c=*cp++ is equivalent to c=++*cp is equivalent to c=(*cp)++ c=*(++cp) c=*(cp++) c=++(*cp) ???

22 Pointer comparison The relational operators ==, !=, and >= are permitted between pointers of the same type Examples: int a[10], *ap; ap = &a[7]; ap < &a[8] is true ap < &a[4] is false

23 Pointer comparison #include const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have address of the first element in pointer */ ptr = var; i = 0; while ( ptr <= &var[MAX - 1] ) { printf("Address of var[%d] = %x\n", i, ptr ); printf("Value of var[%d] = %d\n", i, *ptr ); /* point to the previous location */ ptr++; i++; } return 0; }

24 Pointer comparison The output is something as follow: Address of var[0] = bfdbcb20 Value of var[0] = 10 Address of var[1] = bfdbcb24 Value of var[1] = 100 Address of var[2] = bfdbcb28 Value of var[2] = 200

25 Pointer conversion A pointer of one type can be converted to a pointer of another type by using an explicit cast: int *ip; double *dp; dp= (double *) ip; OR ip = (int*) dp;

26 Pointer and Array C language treats a variable of type “array of T” as “pointer to T”, whose value is the address of the first element of the array char c[MAX], *cp; cp = c;  cp = &c[0]; c=cp or c++; Wrong

27 Pointer and Array In a function, if an array is necessary to be a formal parameter, it can be declared using pointers. Thus, the following functions are equivalent: int max(int a[],int length) { int i,maxv; for(i=1,maxv=a[0];i maxv) maxv=a[i]; return maxv; } int max(int *a,int length) { int i,maxv; for(i=1,maxv=*a;i maxv) maxv=*a; return maxv; }


Download ppt "CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a."

Similar presentations


Ads by Google