chapter 6 arrays, pointers, and strings 2008-11-26

35
Chapter 6 Chapter 6 Arrays, Pointers, and Arrays, Pointers, and Strings Strings 2008-11-26

Post on 21-Dec-2015

225 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Chapter 6 Chapter 6

Arrays, Pointers, and StringsArrays, Pointers, and Strings

2008-11-26

Page 2: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

MemoryMemory

Compiler

User programint money=50;

變數 a ,在某個箱子 ( 位址 ) ,放 50 元變數對應表

Page 3: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

MemoryMemory

2650x0045FF00

520x0045FF04

620x00450008

780x0045000B

…………………………………………………………………………

…………………………………………………………………………

…………………………………………………………………………

0x00000000

0xFFFFFFFF

Page 4: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

PointerPointer

► #include <stdio.h>#include <stdio.h>

► int main(void)int main(void)► {{► int a=100;int a=100;► printf(“a=%d",a);printf(“a=%d",a);► printf(“,location of a=%p",&a); printf(“,location of a=%p",&a); ► }}

Output: a=100,location of a=0023FF74

Page 5: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

PointerPointer

► #include <stdio.h>#include <stdio.h>

► int main(void)int main(void)► {{► int *p = 100;int *p = 100;► printf(“*p=%d”,*p); //printf(“*p=%d”,*p); // 錯誤、程式當機 錯誤、程式當機 ► }}

Output: ErrorMemory

Page 6: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

PointerPointer

► #include <stdio.h>#include <stdio.h>

► int main(void)int main(void)► {{► int a=100;int a=100;► int *p=&a; int *p=&a; ► printf(“\n a = %d”,a); // Value aprintf(“\n a = %d”,a); // Value a► printf(“\n &a = %p”,&a); // Location aprintf(“\n &a = %p”,&a); // Location a► printf(“\n *p = %d",*p); // Value aprintf(“\n *p = %d",*p); // Value a► printf(“\n &p = %p”,&p); // Location pprintf(“\n &p = %p”,&p); // Location p► printf(“\n p = %p”,p); // Location aprintf(“\n p = %p”,p); // Location a► }}

Output:

a = 100&a = 0023FF74*p = 100&p = 0023FF70p = 0023FF74

Page 7: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► #include <stdio.h>#include <stdio.h>► int main(void)int main(void)► {{► int a=100;int a=100;► int b=200;int b=200;► int *p = &a;int *p = &a; ► ► printf("\n a = %d",a); // Value aprintf("\n a = %d",a); // Value a► printf("\n &a = %p",&a); // Location a printf("\n &a = %p",&a); // Location a ► printf("\n b = %d",b); // Value b printf("\n b = %d",b); // Value b ► printf("\n &b = %p",&b); // Location b printf("\n &b = %p",&b); // Location b ► printf("\n *p = %d",*p); // Value aprintf("\n *p = %d",*p); // Value a► printf("\n &p = %p",&p); // Location pprintf("\n &p = %p",&p); // Location p► printf("\n p = %p",p); // Location aprintf("\n p = %p",p); // Location a► printf("\n");printf("\n");

► *p = &b;*p = &b;► printf("\n a = %p",a); // Value a is modifiedprintf("\n a = %p",a); // Value a is modified► printf("\n &a = %p",&a); // Location a printf("\n &a = %p",&a); // Location a ► printf("\n b = %d",b); // Value b printf("\n b = %d",b); // Value b ► printf("\n &b = %p",&b); // Location b printf("\n &b = %p",&b); // Location b ► printf("\n *p = %p",*p); // Location bprintf("\n *p = %p",*p); // Location b► printf("\n &p = %p",&p); // Location pprintf("\n &p = %p",&p); // Location p► printf("\n p = %p",p); // Location aprintf("\n p = %p",p); // Location a► }}

a = 100&a = 0023FF74b = 200&b = 0023FF70*p = 100&p = 0023FF6Cp = 0023FF74

a = 0023FF70&a = 0023FF74b = 200&b = 0023FF70*p = 0023FF70&p = 0023FF6Cp = 0023FF74

Page 8: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

PointerPointer

► int i=3,j=5, *p=&i, *q=&j, *r;int i=3,j=5, *p=&i, *q=&j, *r;► double x;double x;

p==&ip==&i p==(&i)p==(&i) 11

**&p**&p *(*(&p))*(*(&p)) 33

r=&xr=&x r=(&x)r=(&x) /*illegal*//*illegal*/

7**p / *q+77**p / *q+7 (((7*(*p)))/(*q))+7(((7*(*p)))/(*q))+7 1111

Page 9: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

ExampleExample

► void swap(int *p, int *q)void swap(int *p, int *q)► {{► int tmp;int tmp;► tmp = *p;tmp = *p;► *p = *q;*p = *q;► *q = tmp;*q = tmp;► }}

► #include <stdio.h>#include <stdio.h>► void swap(int *, int *);void swap(int *, int *);

► int main (void)int main (void)► {{► int i=4,j=5;int i=4,j=5;► swap(&i,&j);swap(&i,&j);► printf(“i=%d,j=%d”,i,j);printf(“i=%d,j=%d”,i,j);► return 0;return 0;► }}

3i

5j

*p *q

tmp

Output: i=5,j=4

Page 10: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Array addressArray address

► int i,my_array[10];int i,my_array[10];► for (i=0;i<10;i++)for (i=0;i<10;i++)► my_array[i] = i;my_array[i] = i;

► printf(“%p”,my_array); printf(“%p”,my_array); ► printf(“%d”,my_array[0]);printf(“%d”,my_array[0]);► printf(“%p”,&my_array[0]);printf(“%p”,&my_array[0]);► printf(“%d”,my_array[2]);printf(“%d”,my_array[2]);► printf(“%p”,&my_array[2]);printf(“%p”,&my_array[2]);

0023FF3000023FF3020023FF38

Page 11: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► #include <stdio.h>#include <stdio.h>

► int fun(int *p)int fun(int *p)► {{► *p = 70;*p = 70;► }}

► int main(void)int main(void)► {{► int i,my_array[10],sum;int i,my_array[10],sum;► for (i=0;i<10;i++)for (i=0;i<10;i++)► my_array[i] = i;my_array[i] = i;

► fun_sum(my_array);fun_sum(my_array);► fun_sum(&my_array[0]);fun_sum(&my_array[0]);► fun_sum(&my_array[5]);fun_sum(&my_array[5]);► ► }}

Page 12: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

n = 7 20 8030 5040 70 90

0 32 51 4 6

pass 1:

n = 7 20 7040 8030 50 90

0 32 51 4 6

pass 2:

n = 7 20 5040 8030 70 90

0 32 51 4 6

pass 3:

n = 7 20 3040 7090 80 50

0 32 51 4 6

n = 7 20 5040 8030 70 90

0 32 51 4 6

pass 4:

Page 13: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► #include <stdio.h>#include <stdio.h>► void swap(int *p, int *q)void swap(int *p, int *q)► {{► int tmp;int tmp;► tmp = *p;tmp = *p;► *p = *q;*p = *q;► *q = tmp;*q = tmp;► }}► void bubble(int a[], int n)void bubble(int a[], int n)► {{► int i,j;int i,j;► for (i=0;i<n-1;i++)for (i=0;i<n-1;i++)► {{► for (j=0;j<(n-i);j++) for (j=0;j<(n-i);j++) ► { { ► if (a[j] > a[j+1])if (a[j] > a[j+1])► swap(&a[j], &a[j+1]);swap(&a[j], &a[j+1]);► }}► }}► }}► void main()void main()► {{► int a[] = {7, 3, 66, 35, -5, 22, -77, 2};int a[] = {7, 3, 66, 35, -5, 22, -77, 2};► int i;int i;► bubble(a,8); bubble(a,8); ► for(i=0;i<8;i++)for(i=0;i<8;i++)► printf("%d ",a[i]);printf("%d ",a[i]);► }}

Page 14: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

7 3 66 35 -5 22 -77 2

n=8

void bubble(int a[], int n)void bubble(int a[], int n){{ int i,j;int i,j; for (i=0;i<n-1;i++)for (i=0;i<n-1;i++) {{ for (j=0;j<(n-i);j++) for (j=0;j<(n-i);j++) { { if (a[j] > a[j+1])if (a[j] > a[j+1]) swap(&a[j], &a[j+1]);swap(&a[j], &a[j+1]); }} }}}} Page:258

i=0 j=0

Page 15: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Time complexity of bubble sortTime complexity of bubble sort

► A bubble sort is very inefficient. If the size of the A bubble sort is very inefficient. If the size of the array is array is nn, then the number of comparisons , then the number of comparisons performed is proportional to performed is proportional to nn22. .

Page 16: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

6.8 Dynamic Memory Allocation With 6.8 Dynamic Memory Allocation With calloc()calloc() and and malloc()malloc()

► C provides the functions C provides the functions calloc()calloc() and and malloc()malloc() in the standard in the standard library, and their function prototypes are in stdlib.h. library, and their function prototypes are in stdlib.h.

► The name calloc stands for The name calloc stands for “contiguous allocation,”“contiguous allocation,” and the and the name malloc stands for name malloc stands for “memory allocation.”“memory allocation.”

► The programmer uses The programmer uses calloc()calloc() and and malloc()malloc() to dynamically to dynamically create space for arrays, structures(Chap.9), and create space for arrays, structures(Chap.9), and unions(Chap.10). unions(Chap.10).

I can not predict the requirement of user memory

Page 17: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

calloccalloc

► int *a = int *a = calloc(5,sizeof(int));calloc(5,sizeof(int));

► free(a);free(a);

a

*a*a

要 5 塊 int 大小的記憶體空間

特別注意

Page 18: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

mallocmalloc

► int *a = int *a = malloc(5*sizeof(int));malloc(5*sizeof(int));

► free(a);free(a);

a

*a*a

要 5 塊 int 大小的記憶體空間

特別注意

Page 19: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

calloc and malloccalloc and malloc

► calloccalloc is similar to malloc, but the main difference is similar to malloc, but the main difference is that the values stored in the allocated memory is that the values stored in the allocated memory space is space is zerozero by default. With malloc, the allocated by default. With malloc, the allocated memory could have any value. memory could have any value.

► calloccalloc requires requires two argumentstwo arguments. The first is the . The first is the number of variables you'd like to allocate memory number of variables you'd like to allocate memory for. The second is the size of each variable. for. The second is the size of each variable.

Page 20: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Example 1Example 1► #include <stdio.h>#include <stdio.h>► #include <stdlib.h>#include <stdlib.h>

► int main(void)int main(void)► {{► int *a;int *a;► int n=4;int n=4;► ► a = a = calloc(n,sizeof(int));calloc(n,sizeof(int));► ► *a = 10;*a = 10;► *(a+1) = 20;*(a+1) = 20;► ► printf("%d %d",*a,*(a+1));printf("%d %d",*a,*(a+1));

► free(a);free(a);► }}

10

*a*a *(a+1)*(a+1)

20

*(a+2)*(a+2) *(a+3)*(a+3)

Output: 10 20Output: 10 20

Page 21: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► #include <stdio.h>#include <stdio.h>► #include <stdlib.h>#include <stdlib.h>

► int main(void)int main(void)► {{► int *a;int *a;► int n=10;int n=10;► int i;int i;► int sum=0;int sum=0;► a = a = calloc(n,sizeof(int));calloc(n,sizeof(int));► ► for (i=0;i<n;i++)for (i=0;i<n;i++)► {{► *(a+i) = 10;*(a+i) = 10;► sum += *(a+i);sum += *(a+i);► }}► printf("%d",sum);printf("%d",sum);► }}

Example 2 Example 2

Output: 100Output: 100

Page 22: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Example 3Example 3► #include <stdio.h>#include <stdio.h>► #include <stdlib.h>#include <stdlib.h>

► void fun(int *a)void fun(int *a)► {{► *a = 30; *a = 30; ► *(a+1) = 40;*(a+1) = 40;► }}

► void main()void main()► {{► int *a = calloc(5,sizeof(int));int *a = calloc(5,sizeof(int));► *a = 10;*a = 10;► *(a+1) = 20;*(a+1) = 20;► ► printf("\n*a=%d,*(a+1)=%d",*a,*(a+1));printf("\n*a=%d,*(a+1)=%d",*a,*(a+1));► fun(&a[2]); fun(&a[2]); ► printf("\n*a=%d,*(a+1)=%d,*(a+2)=%d,*(a+3)=printf("\n*a=%d,*(a+1)=%d,*(a+2)=%d,*(a+3)=

%d",*a,*(a+1),*(a+2),*(a+3));%d",*a,*(a+1),*(a+2),*(a+3));► free(a);free(a);► }}

10

*a*a *(a+1)*(a+1)

20

*(a+2)*(a+2) *(a+3)*(a+3)

Output:Output:

*(a+3)*(a+3)

*a=10,*(a+1)=20*a=10,*(a+1)=20,*(a+2)=30,*(a+3)=40

Page 23: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Example 4Example 4► #include <stdio.h>#include <stdio.h>► #include <stdlib.h> #include <stdlib.h>

► int main() {int main() {► float *calloc1,*malloc1 ;float *calloc1,*malloc1 ;► int i;int i;

► calloc1 = calloc(3, sizeof(float));calloc1 = calloc(3, sizeof(float));► malloc1 = malloc(3 * sizeof(float));malloc1 = malloc(3 * sizeof(float));

► if(calloc1!=NULL && malloc1!=NULL!=NULL) {if(calloc1!=NULL && malloc1!=NULL!=NULL) {► for(i=0 ; i<3 ; i++) {for(i=0 ; i<3 ; i++) {► printf("calloc1[%d] holds %.0f, ", i, calloc1[i]);printf("calloc1[%d] holds %.0f, ", i, calloc1[i]);► printf("malloc1[%d] holds %.0f\n", i, malloc1[i]);printf("malloc1[%d] holds %.0f\n", i, malloc1[i]);► }}► free(calloc1);free(calloc1);► free(malloc1); free(malloc1); ► }}► else {else {► printf("Not enough memory\n"); printf("Not enough memory\n"); ► }}► }}

Page 24: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Example 5Example 5

► Use Use scanfscanf to get to get nn► Use Use calloccalloc to get the to get the nn memory space memory space► Use Use *a*a to initial the value (0,1,2,3,4,5…..) to initial the value (0,1,2,3,4,5…..)► Use Use for-loopfor-loop to get the sum to get the sum

Page 25: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► #include <stdio.h>#include <stdio.h>► #include <stdlib.h>#include <stdlib.h>

► int main(void)int main(void)► {{► int *a; int *a; ► int i,n,sum=0; int i,n,sum=0; ► while(n!=999)while(n!=999)► {{► printf("\n\nInput n (Esc:999) --> ");printf("\n\nInput n (Esc:999) --> ");► scanf("%d",&n);scanf("%d",&n);► a = calloc(n,sizeof(int));a = calloc(n,sizeof(int));► for (i=0;i<n;i++)for (i=0;i<n;i++)► {{► *(a+i) = i; *(a+i) = i; ► sum += *(a+i);sum += *(a+i);► }}► printf("n=%d,sum=%d",n,sum); printf("n=%d,sum=%d",n,sum); ► sum=0;sum=0;► free(a);free(a);► } } ► }}

Page 26: Chapter 6 Arrays, Pointers, and Strings 2008-11-26
Page 27: Chapter 6 Arrays, Pointers, and Strings 2008-11-26
Page 28: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Example 6Example 6

► Input the array size Input the array size nn, and do the following tasks., and do the following tasks.

► 1 create space for an array of size n1 create space for an array of size n► 2 fill the array with randomly distributed digits2 fill the array with randomly distributed digits► 3 print the array and the sum of its element3 print the array and the sum of its element► 4 release the space4 release the space

Page 29: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► #include <stdio.h>#include <stdio.h>► #include <stdlib.h>#include <stdlib.h>► #include <time.h>#include <time.h>

► void fill_array(int *a, int n);void fill_array(int *a, int n);► int sum_array(int *a, int n);int sum_array(int *a, int n);► void wrt_array(int *a, int n);void wrt_array(int *a, int n);

► void fill_array(int *a, int n)void fill_array(int *a, int n)► {{► int i;int i;► for (i=0;i<n;++i)for (i=0;i<n;++i)► a[i] = rand() % 19 – 9;a[i] = rand() % 19 – 9;► }}

Page 30: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► int sum_array(int *a, int n)int sum_array(int *a, int n)► {{► int i, sum=0;int i, sum=0;► for (i=0;i<n;++i)for (i=0;i<n;++i)► sum += a[i];sum += a[i];► return sum;return sum;► }}

► void wrt_array(int *a, int n)void wrt_array(int *a, int n)► {{► int i;int i;► printf(“a= [“);printf(“a= [“);► for (i=0;i<n;++i)for (i=0;i<n;++i)► printf(“%d%s”,a[i],((i<n-1) ? “,” : “]\n”));printf(“%d%s”,a[i],((i<n-1) ? “,” : “]\n”));► }}

Page 31: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► int main(void)int main(void)► {{► int *a,n;int *a,n;► srand(time(NULL)); /*seed the random number generator*/srand(time(NULL)); /*seed the random number generator*/► printf(“\n%s\n”,printf(“\n%s\n”,► “ “This program does the following repeatedly:\n”This program does the following repeatedly:\n”► “ “\n”\n”► “ “ 1 create space for an array of size n\n”1 create space for an array of size n\n”► “ “ 2 fill the array with randomly distributed digits\n”2 fill the array with randomly distributed digits\n”► “ “ 3 print the array and the sum of its element\n”3 print the array and the sum of its element\n”► “ “ 4 release the space\n”);4 release the space\n”);

Page 32: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

► for ( ; ;)for ( ; ;)► {{► printf(“Input n: “);printf(“Input n: “);► if (scanf(“%d”,&n) != 1 || n< 1)if (scanf(“%d”,&n) != 1 || n< 1)► break;break;► putchar(‘\n’);putchar(‘\n’);► a = calloc(n,sizeof(int));a = calloc(n,sizeof(int));► fill_array(a,n);fill_array(a,n);► wrt_array(a,n);wrt_array(a,n);► printf(“sum=%d\n”,sum_array(a,n));printf(“sum=%d\n”,sum_array(a,n));► free(a);free(a);► }}► printf(“\nBYE-BYE!!”);printf(“\nBYE-BYE!!”);► return 0;return 0;► }}

Page 33: Chapter 6 Arrays, Pointers, and Strings 2008-11-26
Page 34: Chapter 6 Arrays, Pointers, and Strings 2008-11-26
Page 35: Chapter 6 Arrays, Pointers, and Strings 2008-11-26

Offsetting the PointerOffsetting the Pointer

► For arrays (vectors) intended for mathematical use, For arrays (vectors) intended for mathematical use, we often want to index the arrays from 1 instead 0. we often want to index the arrays from 1 instead 0.

► We can do something like the following:We can do something like the following:

int n;int n; double *a;double *a; …… a = calloc(n+1, sizeof(double));a = calloc(n+1, sizeof(double));

► We can disregard a[0] and use a[1],…,a[n] as We can disregard a[0] and use a[1],…,a[n] as needed.needed.