Programming Language B Takako Nemoto (JAIST) 7 January Takako Nemoto (JAIST) 7 January 1 / 13
Usage of pointers #include <stdio.h> int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko; isako = &sato; hiroko = &masaki; printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); isako = &sanaka; *hiroko = 180; printf("sato = %d\n", sato); printf("sanaka = %d\n", sanaka); printf("masaki = %d\n", masaki); printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); Takako Nemoto (JAIST) 7 January 2 / 13
Usage of pointers #include <stdio.h> int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko; isako = &sato; hiroko = &masaki; printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); isako = &sanaka; *hiroko = 180; printf("sato = %d\n", sato); printf("sanaka = %d\n", sanaka); printf("masaki = %d\n", masaki); printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); Variables declared with * is called pointer. In this example, isako and hiroko are pointers. Their type are called pointer type to int-type object, pointer type to int, or int * type. If you declare them as int *isako, hiroko then hiroko has int type. Takako Nemoto (JAIST) 7 January 2 / 13
Usage of pointers #include <stdio.h> int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko; isako = &sato; hiroko = &masaki; printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); isako = &sanaka; *hiroko = 180; printf("sato = %d\n", sato); printf("sanaka = %d\n", sanaka); printf("masaki = %d\n", masaki); printf("*isako = %d\n", *isako); printf("*hiroko = %d\n", *hiroko); Pointers has the value of addresses of the objects with declared type. When the address of x is assigned into a pointer p, we say p points x. In this example, isako points sato and hiroko points first masaki and changed to sanaka. Unary * operator is applied to pointers and takes the object the pointer points. If the pointer p points the object x, then *p is an alias (alternative name) for x. E.g., *hiroko = 180; has the same meaning masaki = 180;. Takako Nemoto (JAIST) 7 January 3 / 13
Pointers and arrays #include <stdio.h> //10-9.c int a[5] = {1,2,3,4,5; int *p = a; for(i = 0; i < 5; i++) printf("&a[%d] = %p p+%d = %p\n", i, &a[i], i, p + i); int *p = a; is same as int *p = &a[0];. p + i is the pointer to a[i]. (Quiz 12) Change the 5th line as *p = &a[2]; and check the value of p - 2, p - 1, p, p + 1, p + 2 using printf. Send the screen shot of the execution result. Takako Nemoto (JAIST) 7 January 4 / 13
Pointers and arrays #include <stdio.h> //10-9.c int a[5] = {1,2,3,4,5; int *p = a; for(i = 0; i < 5; i++) printf("&a[%d] = %p p+%d = %p\n", i, &a[i], i, p + i); An array is recognized as a pointer to its first element, except for the following two cases: sizeof(a) is the size of array a itself, not the size of the pointer to a s first element. & a is not the pointer to the first element of a, but the pointer to array a in total. Recall the following does not work: int a[5] = {1,2,3,4,5; int b[5]; a = b; but we can copy them as in the example. Takako Nemoto (JAIST) 7 January 5 / 13
Pointers and arrays #include <stdio.h> int a[5] = {1,2,3,4,5; int *p = a; Recall that if the address of var is assigned to a pointer p, then *p is an alias of var. Similarly, *(p+i) is an alias of a[i] in the example. for(i = 0; i < 5; i++) printf("a[%d] = %d *(a+%d) = %d p[%d] = %d *(p+%d) = %d\n", i, a[i], i, *(a+i), i, p[i], i, *(p + i)); for(i = 0; i < 5; i++) printf("&a[%d] = %p a+%d = %p &p[%d] = %p p+%d = %p\n", i, &a[i], i, (a+i), i, &p[i], i, (p + i)); Takako Nemoto (JAIST) 7 January 6 / 13
Functions and pointer #include <stdio.h> void ary_set(int v[], int n, int val){ for (i = 0; i < n; i++) v[i] = val; int a[] = {1, 2, 3, 4, 5; The Parameter v[] in ary_set is recognized as *v. Even v[] is replaced by v[5], the length 5 is ignored. This is why we have to set another parameter n for the length of v. ary_set(a, 5, 99); for (i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); Takako Nemoto (JAIST) 7 January 7 / 13
Some more examples #include <stdio.h> //ex10-5.c void ary_set(int v[], int n, int val){ for (i = 0; i < n; i++) v[i] = val; int a[] = {1, 2, 3, 4, 5; ary_set(&a[2], 2, 99); for (i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); Takako Nemoto (JAIST) 7 January 8 / 13
Some more examples #include <stdio.h> //ex10-5.c void ary_set(int v[], int n, int val){ for (i = 0; i < n; i++) v[i] = val; int a[] = {1, 2, 3, 4, 5; In the function call ary_set(&a[2], 2, 99) the pointer to a[2] and the length 2 are given as arguments. Hence only the value of a[2] and a[3] are changed. ary_set(&a[2], 2, 99); for (i = 0; i < 5; i++) printf("a[%d] = %d\n", i, a[i]); Takako Nemoto (JAIST) 7 January 9 / 13
Some more examples #include <stdio.h> //10c-1.c int i, a[4]; 0[a] = a[1] = *(a + 2) = *(a + 3) = 7; for (i = 0; i < 4; i++) printf("a[%d] = %d\n", i, a[i]); [] is a commutative operator! Takako Nemoto (JAIST) 7 January 10 / 13
Advanced topic #include <stdio.h> //malloc-1.c int n; puts("input an integer: "); scanf("%d", &n); int v[n]; for (i = 0; i < n; i++) v[i] = i; for (i=0;i < n; i++) printf("%d ", v[i]); printf("\n"); This example might not work, depending on compilers. The reason is that static allocations are used for arrays. Takako Nemoto (JAIST) 7 January 11 / 13
Advanced topic #include <stdio.h> //malloc-2.c #include <stdlib.h> int main(void) { int *v; int i, n; printf("input a number : "); scanf("%d", &n); v = (int *)malloc(n * sizeof(int)); if (v == NULL) exit(0); for (i=0; i<n; i++) v[i] = i; for (i=0;i < n; i++) printf("%d ", v[i]); printf("\n"); free(v); malloc is a function which keeps dynamic memory of designated size, contained in stdlib.h. If it fails to take memory allocations, it returns NULL and the program does not work. Then you can set the size of arrays as in the example. To use malloc, you should check the return value of malloc like if (v == NULL) exit(0); and HAVE TO clear the memory allocation in the end by free(v); Takako Nemoto (JAIST) 7 January 12 / 13
Today s homework 1 Do Exercise 10-2 ( 10-2): Make function to renew the date to the previous one and to the next one. Do not forget bissextile years: decrement_date(int *y, int *m, int *d){... increment_date(int *y, int *m, int *d){... 2 Do Exercise 10-4 ( 10-4): Make a function set_idx(int *v, int n){... such that, for the input of an array with n int-type elements, it assigns the value same as the index to each element of the array. 3 Read sections 10 and 11. As usual, send the programs and the text file as attached files to me via e-mail, with the title Homework Lecture 12 by the next lecture. Please make sure to include your name and student ID number. Takako Nemoto (JAIST) 7 January 13 / 13