350 likes | 398 Views
Chapter 6. Arrays, Pointers, and Strings. 2008-11-26. Memory. Compiler. int money=50;. 變數對應表. User program. 變數 a ,在某個箱子 ( 位址 ) ,放 50 元. 0x00000000. Memory. …………………………………………………………………………. 78. 265. 52. 62. 0x0045000B. 0x0045FF00. 0x0045FF04. 0x00450008.
E N D
Chapter 6 Arrays, Pointers, and Strings 2008-11-26
Memory Compiler int money=50; 變數對應表 User program 變數a,在某個箱子(位址),放50元
0x00000000 Memory ………………………………………………………………………… 78 265 52 62 0x0045000B 0x0045FF00 0x0045FF04 0x00450008 ………………………………………………………………………… ………………………………………………………………………… 0xFFFFFFFF
Pointer • #include <stdio.h> • int main(void) • { • int a=100; • printf(“a=%d",a); • printf(“,location of a=%p",&a); • } Output: a=100,location of a=0023FF74
Pointer • #include <stdio.h> • int main(void) • { • int *p = 100; • printf(“*p=%d”,*p); //錯誤、程式當機 • } Memory Output: Error
Pointer • #include <stdio.h> • int main(void) • { • int a=100; • int *p=&a; • printf(“\n a = %d”,a); // Value a • printf(“\n &a = %p”,&a); // Location a • printf(“\n *p = %d",*p); // Value a • printf(“\n &p = %p”,&p); // Location p • printf(“\n p = %p”,p); // Location a • } a = 100 &a = 0023FF74 *p = 100 &p = 0023FF70 p = 0023FF74 Output:
#include <stdio.h> • int main(void) • { • int a=100; • int b=200; • int *p = &a; • printf("\n a = %d",a); // Value a • printf("\n &a = %p",&a); // Location a • printf("\n b = %d",b); // Value b • printf("\n &b = %p",&b); // Location b • printf("\n *p = %d",*p); // Value a • printf("\n &p = %p",&p); // Location p • printf("\n p = %p",p); // Location a • printf("\n"); • *p = &b; • printf("\n a = %p",a); // Value a is modified • printf("\n &a = %p",&a); // Location a • printf("\n b = %d",b); // Value b • printf("\n &b = %p",&b); // Location b • printf("\n *p = %p",*p); // Location b • printf("\n &p = %p",&p); // Location p • printf("\n p = %p",p); // Location a • } a = 100 &a = 0023FF74 b = 200 &b = 0023FF70 *p = 100 &p = 0023FF6C p = 0023FF74 a = 0023FF70 &a = 0023FF74 b = 200 &b = 0023FF70 *p = 0023FF70 &p = 0023FF6C p = 0023FF74
Pointer • int i=3,j=5, *p=&i, *q=&j, *r; • double x;
Example • #include <stdio.h> • void swap(int *, int *); • int main (void) • { • int i=4,j=5; • swap(&i,&j); • printf(“i=%d,j=%d”,i,j); • return 0; • } • void swap(int *p, int *q) • { • int tmp; • tmp = *p; • *p = *q; • *q = tmp; • } tmp j i 5 3 *q *p Output: i=5,j=4
Array address • int i,my_array[10]; • for (i=0;i<10;i++) • my_array[i] = i; • printf(“%p”,my_array); • printf(“%d”,my_array[0]); • printf(“%p”,&my_array[0]); • printf(“%d”,my_array[2]); • printf(“%p”,&my_array[2]); 0023FF30 0 0023FF30 2 0023FF38
#include <stdio.h> • int fun(int *p) • { • *p = 70; • } • int main(void) • { • int i,my_array[10],sum; • for (i=0;i<10;i++) • my_array[i] = i; • fun_sum(my_array); • fun_sum(&my_array[0]); • fun_sum(&my_array[5]); • }
pass 4: pass 3: pass 2: pass 1: n = 7 n = 7 n = 7 n = 7 n = 7 20 20 20 20 20 40 30 90 30 30 40 40 40 30 40 50 30 70 80 50 70 50 80 70 70 70 80 80 80 50 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 90 50 90 90 90
#include <stdio.h> • void swap(int *p, int *q) • { • int tmp; • tmp = *p; • *p = *q; • *q = tmp; • } • void bubble(int a[], int n) • { • int i,j; • for (i=0;i<n-1;i++) • { • for (j=0;j<(n-i);j++) • { • if (a[j] > a[j+1]) • swap(&a[j], &a[j+1]); • } • } • } • void main() • { • int a[] = {7, 3, 66, 35, -5, 22, -77, 2}; • int i; • bubble(a,8); • for(i=0;i<8;i++) • printf("%d ",a[i]); • }
n=8 7 3 66 35 -5 22 -77 2 i=0 j=0 void bubble(int a[], int n) { int i,j; for (i=0;i<n-1;i++) { for (j=0;j<(n-i);j++) { if (a[j] > a[j+1]) swap(&a[j], &a[j+1]); } } } Page:258
Time complexity of bubble sort • A bubble sort is very inefficient. If the size of the array is n, then the number of comparisons performed is proportional to n2.
6.8 Dynamic Memory Allocation With calloc() and malloc() • C provides the functions calloc() and malloc() in the standard library, and their function prototypes are in stdlib.h. • The name calloc stands for “contiguous allocation,” and the name malloc stands for “memory allocation.” • The programmer uses calloc() and malloc() to dynamically create space for arrays, structures(Chap.9), and unions(Chap.10). I can not predict the requirement of user memory
calloc 要5塊int大小的記憶體空間 • int *a = calloc(5,sizeof(int)); • free(a); *a a 特別注意
malloc 要5塊int大小的記憶體空間 • int *a = malloc(5*sizeof(int)); • free(a); *a a 特別注意
calloc and malloc • calloc is similar to malloc, but the main difference is that the values stored in the allocated memory space is zero by default. With malloc, the allocated memory could have any value. • calloc requires two arguments. The first is the number of variables you'd like to allocate memory for. The second is the size of each variable.
Example 1 10 20 • #include <stdio.h> • #include <stdlib.h> • int main(void) • { • int *a; • int n=4; • a = calloc(n,sizeof(int)); • *a = 10; • *(a+1) = 20; • printf("%d %d",*a,*(a+1)); • free(a); • } *(a+1) *a *(a+2) *(a+3) Output: 10 20
Example 2 • #include <stdio.h> • #include <stdlib.h> • int main(void) • { • int *a; • int n=10; • int i; • int sum=0; • a = calloc(n,sizeof(int)); • for (i=0;i<n;i++) • { • *(a+i) = 10; • sum += *(a+i); • } • printf("%d",sum); • } Output: 100
Example 3 10 20 • #include <stdio.h> • #include <stdlib.h> • void fun(int *a) • { • *a = 30; • *(a+1) = 40; • } • void main() • { • int *a = calloc(5,sizeof(int)); • *a = 10; • *(a+1) = 20; • printf("\n*a=%d,*(a+1)=%d",*a,*(a+1)); • fun(&a[2]); • printf("\n*a=%d,*(a+1)=%d,*(a+2)=%d,*(a+3)=%d",*a,*(a+1),*(a+2),*(a+3)); • free(a); • } *(a+1) *a *(a+2) *(a+3) *(a+3) *a=10,*(a+1)=20 *a=10,*(a+1)=20,*(a+2)=30,*(a+3)=40 Output:
Example 4 • #include <stdio.h> • #include <stdlib.h> • int main() { • float *calloc1,*malloc1 ; • int i; • calloc1 = calloc(3, sizeof(float)); • malloc1 = malloc(3 * sizeof(float)); • if(calloc1!=NULL && malloc1!=NULL!=NULL) { • for(i=0 ; i<3 ; i++) { • printf("calloc1[%d] holds %.0f, ", i, calloc1[i]); • printf("malloc1[%d] holds %.0f\n", i, malloc1[i]); • } • free(calloc1); • free(malloc1); • } • else { • printf("Not enough memory\n"); • } • }
Example 5 • Use scanf to get n • Use calloc to get the n memory space • Use *a to initial the value (0,1,2,3,4,5…..) • Use for-loop to get the sum
#include <stdio.h> • #include <stdlib.h> • int main(void) • { • int *a; • int i,n,sum=0; • while(n!=999) • { • printf("\n\nInput n (Esc:999) --> "); • scanf("%d",&n); • a = calloc(n,sizeof(int)); • for (i=0;i<n;i++) • { • *(a+i) = i; • sum += *(a+i); • } • printf("n=%d,sum=%d",n,sum); • sum=0; • free(a); • } • }
Example 6 • Input the array size n, and do the following tasks. • 1 create space for an array of size n • 2 fill the array with randomly distributed digits • 3 print the array and the sum of its element • 4 release the space
#include <stdio.h> • #include <stdlib.h> • #include <time.h> • void fill_array(int *a, int n); • int sum_array(int *a, int n); • void wrt_array(int *a, int n); • void fill_array(int *a, int n) • { • int i; • for (i=0;i<n;++i) • a[i] = rand() % 19 – 9; • }
int sum_array(int *a, int n) • { • int i, sum=0; • for (i=0;i<n;++i) • sum += a[i]; • return sum; • } • void wrt_array(int *a, int n) • { • int i; • printf(“a= [“); • for (i=0;i<n;++i) • printf(“%d%s”,a[i],((i<n-1) ? “,” : “]\n”)); • }
int main(void) • { • int *a,n; • srand(time(NULL)); /*seed the random number generator*/ • printf(“\n%s\n”, • “This program does the following repeatedly:\n” • “\n” • “ 1 create space for an array of size n\n” • “ 2 fill the array with randomly distributed digits\n” • “ 3 print the array and the sum of its element\n” • “ 4 release the space\n”);
for ( ; ;) • { • printf(“Input n: “); • if (scanf(“%d”,&n) != 1 || n< 1) • break; • putchar(‘\n’); • a = calloc(n,sizeof(int)); • fill_array(a,n); • wrt_array(a,n); • printf(“sum=%d\n”,sum_array(a,n)); • free(a); • } • printf(“\nBYE-BYE!!”); • return 0; • }
Offsetting the Pointer • For arrays (vectors) intended for mathematical use, we often want to index the arrays from 1 instead 0. • We can do something like the following: • int n; • double *a; • … • a = calloc(n+1, sizeof(double)); • We can disregard a[0] and use a[1],…,a[n] as needed.