Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name cannot be changed by an assignment statement or by any other means. A pointer is a variable whose address value can be changed by assignment.
Using Array Name float on_time_rate[100]; float *ptr; ptr = on_time_rate; Or equivalently ptr = &on_time_rate[0]; Once ptr is assigned value, you can use ptr as an array, e.g., ptr[0], ptr[1], …, ptr[99] This is the same as *ptr, *(ptr+1), …, *(ptr+99)
Pointer to char and Array of char char *s = "My Brother"; Declares a pointer s to char, initializes it to the starting address of a string. char c[]= "My Car"; Declares an array c of size 7, with c[0]='M', c[1]='y', …, c[5]='r', c[6] ='\0'. Then we can say s = c; Or equivalently s = &c[0]; But NOT c = "My hand"; or c = s;
Use pointer without Initialization is an error char *s; scanf("%s", s); Error: pointer has no value! Correct one should be: char *s; char str[10]; s = str; scanf("%s", s); or scanf("%s", str); or scanf("%s", &str[0]); But scanf("%s", &s); is wrong.
Equivalence between Array Indexing and Pointer Dereferencing If p is either and array name or pointer, then the following two forms are equivalent: p[k] *(p+k) In particular p[0] *p Thus, we can use pointer as if it were an array, or array as if it were pointer. The only difference is that array name is a constant, while pointer is a variable.
Pointer Operation The following pointer operations are allowed: –Adding an integer to a pointer, e.g., p + 2. –Subtracting an integer from a pointer, e.g., p - 1. –Subtracting two pointers, e.g., p1 - p2. –Comparing pointers using relational operator, e.g., p1 >= p2. p+1 means the address of the next cell of type which p is pointing to, I.e., if *p is p[0], then *(p+1) is p[1].
Pointer Arithmetic #include main() { char alpha[]={'A', 'B', 'C', 'D', 'E'}; char x, *p1, *p2; p1 = alpha; p2 = p1 + 2; x = *p2; printf("%c%c%c", x, *p1, *(p2-1)); } The program prints: CAB
Compute the String Length Again #include main() { printf("%d\n", length("This")); } int length(char *s) { char *p = s; while(*s) ++s; return (s-p); }
Call by Value and Call by Reference Call by value: The function works with a copy of the argument sent to it. Call by reference: The function receives reference to variable and works directly with the original variable. C programming language does not support call by reference. But its effect can be simulated with pointer.
Simulate Call by Reference #include void f(int x, int *p); main() { int x = 998; int y = 998; f(x,&y); printf("%d %d\n", x, y); } void f(int x, int *p) { x += 2; *p += 2; } The program prints:
Reversing a String in Place #include void rev(char *s); main() { char str[101]; scanf("%s", str); rev(str); printf("%s", str); } void rev(char *s) { char t, *e; e = s + strlen(s) -1; while(s < e) { t = *s; *s++ = *e; *e-- = t; }
Pointers and Multidimensional Array The equivalence *(p+k) p[k] applies also for multidimensional array. Thus we have **q *q[0] q[0][0] q[i][j] *(q[i]+j) *(*(q+i)+j) In particular, if we declare int q[5][5]; Then q is a pointer to pointer to int value.
Mixing Array Indexing and Pointer Dereferencing #include main() { int q[3][3] = {{0,1,2},{3,4,5}, {6,7,8}}; printf("%d %d %d %d\n", q[1][2], *(q[1]+2), *( (*(q+1) + 2) ), *(&q[0][0] + 5) ); } Prints: 5 5
Fixed Size Two- Dimensional Array #include void print_names(char p[][11], int n); main() { char phys[4][11] = {"Newton", "Einstein", "Fermi", "Heisenberg"}; print_names(phys, 4); } void print_names(char p[][11], int n) { int i; for(i = 0; i < n; i++) printf("%s\n", *p++); }
Array of Pointers #include void print_names(char *p[], int n); main() { char *phys[4] = {"Newton", "Einstein", "Fermi", "Heisenberg"}; print_names(phys, 4); } void print_names(char *p[], int n) { int i; for(i = 0; i < n; i++) printf("%s\n", *p++); }
Reading/Home Working Read Chapter 7, page 321 to 379. Work on Problems –Section 7.3, page 337, exercise 1, 3, 5, 7. –Section 7.4, page 349, exercise 1, 3, 5. –Section 7.6, page 367, exercise 3, 5. Check your answers in the back of the textbook. Do not hand in.