C Program Design C Pointers

Preview:

DESCRIPTION

C Program Design C Pointers. 主講人:虞台文. Content. Introduction Point Operators Calling Functions by References Pointer Expressions and Pointer Arithmetic Pointers and Arrays Using the const Qualifier with Pointers Arrays of Pointers. C Program Design C Pointers. Introduction. - PowerPoint PPT Presentation

Citation preview

C Program DesignC Pointers

主講人:虞台文

Content Introduction Point Operators Calling Functions by References Pointer Expressions and Pointer

Arithmetic Pointers and Arrays Using the const Qualifier with Pointers Arrays of Pointers

C Program DesignC Pointers

Introduction

What is Pointer?

CPU and Memory

Memory Space

00000000

00000001

00000002

00000003

00000004

00000005

00000006

00000007

00000008

FFFFFFFB

FFFFFFFC

FFFFFFFD

FFFFFFFE

FFFFFFFF

address Program and data are stored in some places of memory.

Addresses (Pointers) are used to identify where the program or data are stored.

CPU accesses a piece of memory data by emitting its address on address bus.

C Pointers

Powerful, but difficult to master

Simulate call-by-reference– C only has call-by-value

Close relationship with arrays and strings– important for handling arrays and strings

C Program DesignC Pointers

Pointer Variable Definitions

and Initialization

Pointer Variables

Pointer variables contain memory addresses as their values

Indirect reference – Typed pointers point to the address of a

variable of a specific type– Non-typed pointers are simply addresses,

i.e., they can point to an addresses of any thing

Variable is a piece of memory containing a value of a specific type (direct reference).

Pointer Definitions

datatype *pointerName

Examples:

int *ptr1;

int *ptr2, *ptr3;

char *ptr4;

double *ptr5;

void *ptr6, *ptr7;

Typed pointers

Non-typed pointers

Pointer Definitions

datatype *pointerName

Examples:

int *ptr1;

int *ptr2, *ptr3;

char *ptr4;

double *ptr5;

void *ptr6, *ptr7;

Typed pointers

Non-typed pointers

Don’t write asint *ptr2, ptr3;

Don’t write asint *ptr2, ptr3;

ptr1ptr2ptr3ptr4ptr5ptr6ptr7

Pointer Initialization

int *ptr1;

int *ptr2, *ptr3;

char *ptr4;

double *ptr5;

void *ptr6, *ptr7;

Where these pointers point to?

ptr1ptr2ptr3ptr4ptr5ptr6ptr7

Pointer Initialization

#define NULL 0

int *ptr1=NULL;

int *ptr2=NULL, *ptr3=NULL;

char *ptr4=NULL;

double *ptr5=NULL;

void *ptr6=NULL, *ptr7=NULL;

#define NULL 0

int *ptr1=NULL;

int *ptr2=NULL, *ptr3=NULL;

char *ptr4=NULL;

double *ptr5=NULL;

void *ptr6=NULL, *ptr7=NULL;

Before pointers are actually used, it is a good habit to initialize them to NULL, meaning that they are pointing to nothing.

C Program DesignC Pointers

Pointer Operators

Point Operators

Reference operator (&) – Retrieve the address of a variable

Dereference operator (*)– Retrieve the value pointed by a pointer

(address)

* and & are inverses – They cancel each other out

Reference Operator (*)

&: Returns address-of operand

5

3.14

n

x

nPtrxPtr

...

Nickname: address-of operator

int n=5;double x=3.14;. . . . .int *nPtr;double *xPtr;

nPtr = &n;xPtr = &x;

範例:

5

3.14

n

x

nPtrxPtr

...

#include <stdio.h>

int n=5;double x=3.14;

int *nPtr;double *xPtr;

main(){ /* nPtr gets address of n */ nPtr = &n;

/* xPtr gets address of n */ xPtr = &x; printf("Address of n is %p\n" "Address of x is %p\n", nPtr, xPtr);}

#include <stdio.h>

int n=5;double x=3.14;

int *nPtr;double *xPtr;

main(){ /* nPtr gets address of n */ nPtr = &n;

/* xPtr gets address of n */ xPtr = &x; printf("Address of n is %p\n" "Address of x is %p\n", nPtr, xPtr);}

00427600

00427608

00427600

00427608

練習:

5

3.14

n

x

nPtrxPtr

...

#include <stdio.h>

int n=5;double x=3.14;

int *nPtr;double *xPtr;

main(){ /* nPtr gets address of n */ nPtr = &n;

/* xPtr gets address of n */ xPtr = &x; printf("Address of n is %p\n" "Address of x is %p\n", nPtr, xPtr);}

#include <stdio.h>

int n=5;double x=3.14;

int *nPtr;double *xPtr;

main(){ /* nPtr gets address of n */ nPtr = &n;

/* xPtr gets address of n */ xPtr = &x; printf("Address of n is %p\n" "Address of x is %p\n", nPtr, xPtr);}

00427600

00427608

00427600

00427608

1. 將上例之變數改成 main之區域變數,觀察其值,並繪製類似右側之圖。

Dereference Operator (*)

5

3.14

n

x

nPtrxPtr

...

00427600

00427608

00427600

00427608

Nickname: value-point-by operator

*: Returns value point by operand

*nPtr = ?

*xPtr = ?

Dereference Operator (*)

5

3.14

n

x

nPtrxPtr

...

00427600

00427608

00427600

00427608

Nickname: value-point-by operator

*: Returns value point by operand

*nPtr = ?

*xPtr = ?

#include <stdio.h>

int n=5;double x=3.14;

int *nPtr;double *xPtr;

main(){ /* nPtr gets address of n */ nPtr = &n;

/* xPtr gets address of n */ xPtr = &x; printf("integer value in address %p is %d\n" "double value in address %p is %lf\n", nPtr, *nPtr, xPtr, *xPtr);}

#include <stdio.h>

int n=5;double x=3.14;

int *nPtr;double *xPtr;

main(){ /* nPtr gets address of n */ nPtr = &n;

/* xPtr gets address of n */ xPtr = &x; printf("integer value in address %p is %d\n" "double value in address %p is %lf\n", nPtr, *nPtr, xPtr, *xPtr);}

範例:#include <stdio.h>

int main( void ){ int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */

printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr );

printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr );

printf( "\n\nShowing that * and & are complements of " "each other\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; /* indicates successful termination */} /* end main */

#include <stdio.h>

int main( void ){ int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */

printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr );

printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr );

printf( "\n\nShowing that * and & are complements of " "each other\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; /* indicates successful termination */} /* end main */

Showing that * and & are complements of each other.

範例:#include <stdio.h>

int main( void ){ int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */

printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr );

printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr );

printf( "\n\nShowing that * and & are complements of " "each other\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; /* indicates successful termination */} /* end main */

#include <stdio.h>

int main( void ){ int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */

printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr );

printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr );

printf( "\n\nShowing that * and & are complements of " "each other\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; /* indicates successful termination */} /* end main */

Showing that * and & are complements of each other.

0012FF7C7a

aPtr 0012FF7C

練習:#include <stdio.h>

int main( void ){ int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */

printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr );

printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr );

printf( "\n\nShowing that * and & are complements of " "each other\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; /* indicates successful termination */} /* end main */

#include <stdio.h>

int main( void ){ int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */

a = 7; aPtr = &a; /* aPtr set to address of a */

printf( "The address of a is %p" "\nThe value of aPtr is %p", &a, aPtr );

printf( "\n\nThe value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr );

printf( "\n\nShowing that * and & are complements of " "each other\n&*aPtr = %p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr ); return 0; /* indicates successful termination */} /* end main */

0012FF7C7a

aPtr 0012FF7C

1. What is the data type of &aPtr?

2. Modify the following program to also show the address of aPtr.

C Program DesignC Pointers

Calling Functions

by Reference

C Simple Swap Program

#include <stdio.h>

main(){ int a = 23, b = 47; int t;

printf("Before. a: %d, b: %d\n", a, b);

t = a; a = b; b = t;

printf("After. a: %d, b: %d\n", a, b);}

#include <stdio.h>

main(){ int a = 23, b = 47; int t;

printf("Before. a: %d, b: %d\n", a, b);

t = a; a = b; b = t;

printf("After. a: %d, b: %d\n", a, b);}

C Simple Swap Program

#include <stdio.h>

main(){ int a = 23, b = 47; int t;

printf("Before. a: %d, b: %d\n", a, b);

t = a; a = b; b = t;

printf("After. a: %d, b: %d\n", a, b);}

#include <stdio.h>

main(){ int a = 23, b = 47; int t;

printf("Before. a: %d, b: %d\n", a, b);

t = a; a = b; b = t;

printf("After. a: %d, b: %d\n", a, b);}

define a function to do swapping.

C Simple Swap Program

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(a, b);

printf("After. a: %d, b: %d\n", a, b);}

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(a, b);

printf("After. a: %d, b: %d\n", a, b);}

void swap(int a, int b){ int t;

t = a; a = b; b = t;}

void swap(int a, int b){ int t;

t = a; a = b; b = t;}

Workable?

C Simple Swap Program

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(a, b);

printf("After. a: %d, b: %d\n", a, b);}

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(a, b);

printf("After. a: %d, b: %d\n", a, b);}

void swap(int a, int b){ int t;

t = a; a = b; b = t;}

void swap(int a, int b){ int t;

t = a; a = b; b = t;}

Workable?

Calling Function by Passing References

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(&a, &b);

printf("After. a: %d, b: %d\n", a, b);}

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(&a, &b);

printf("After. a: %d, b: %d\n", a, b);}

void swap(int *a, int *b){ int t;

t = *a; *a = *b; *b = t;}

void swap(int *a, int *b){ int t;

t = *a; *a = *b; *b = t;}

Workable?

Calling Function by Passing References

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(&a, &b);

printf("After. a: %d, b: %d\n", a, b);}

#include <stdio.h>

main(){ int a = 23, b = 47;

printf("Before. a: %d, b: %d\n", a, b);

swap(&a, &b);

printf("After. a: %d, b: %d\n", a, b);}

void swap(int *a, int *b){ int t;

t = *a; *a = *b; *b = t;}

void swap(int *a, int *b){ int t;

t = *a; *a = *b; *b = t;}

Workable?

void BubbleSort(int data[], int n)

{

int tmp, i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1]){

tmp = data[j];

data[j] = data[j+1];

data[j+1] = tmp;

}

}

void BubbleSort(int data[], int n)

{

int tmp, i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1]){

tmp = data[j];

data[j] = data[j+1];

data[j+1] = tmp;

}

}

Review: Bubble Sort

void BubbleSort(int data[], int n)

{

int i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1])

swap(&data[j], &data[j+1]);

}

void BubbleSort(int data[], int n)

{

int i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1])

swap(&data[j], &data[j+1]);

}

Bubble Sort

void swap(int *a, int *b){ int t;

t = *a; *a = *b; *b = t;}

void swap(int *a, int *b){ int t;

t = *a; *a = *b; *b = t;}

void swap(int *a, int *b){ int t; t = *a; *a = *b; *b = t;}

void BubbleSort(int data[], int n)

{

int i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1])

swap(&data[j], &data[j+1]);

}

main(){ int vals[5]={100, 30, 20, 75, 15};

BubbleSort(vals, 5); /* ............................ */}

void swap(int *a, int *b){ int t; t = *a; *a = *b; *b = t;}

void BubbleSort(int data[], int n)

{

int i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1])

swap(&data[j], &data[j+1]);

}

main(){ int vals[5]={100, 30, 20, 75, 15};

BubbleSort(vals, 5); /* ............................ */}

Pointers vs. Arrays

100100

3030

2020

7575

1515

01234

vals

BubbleSort(&vals[0], 5);

int * int *

int *

void swap(int *a, int *b){ int t; t = *a; *a = *b; *b = t;}

void BubbleSort(int data[], int n)

{

int i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1])

swap(&data[j], &data[j+1]);

}

main(){ int vals[5]={100, 30, 20, 75, 15};

BubbleSort(vals, 5); /* ............................ */}

void swap(int *a, int *b){ int t; t = *a; *a = *b; *b = t;}

void BubbleSort(int data[], int n)

{

int i, j;

for(i=0; i<n-1; i++)

for(j=0; j<n-i-1; j++)

if(data[j] > data[j+1])

swap(&data[j], &data[j+1]);

}

main(){ int vals[5]={100, 30, 20, 75, 15};

BubbleSort(vals, 5); /* ............................ */}

Pointers vs. Arrays

100100

3030

2020

7575

1515

01234

vals

BubbleSort(&vals[0], 5);

int *

int *

void BubbleSort(int *data, int n)

C Program DesignC Pointers

Pointer Expressions

and Pointer Arithmetic

Pointer Arithmetic

Increment/decrement pointer – ++ or –-

Add/substract an integer to a pointer– + or += , - or -=

Pointers may be subtracted from each other

Pointer Arithmetic

Increment/decrement pointer – ++ or –-

Add/substract an integer to a pointer– + or += , - or -=

Pointers may be subtracted from each other

May be meaningless unless performed on an array.

May be meaningless unless performed on an array.

Pointer Arithmetic

Increment/decrement pointer (++ or –-) Add/substract an integer to a pointer (+ or += , - or -=) Pointers may be subtracted from each other

int data[10];int i;

for(i=0; i<10; i++) data[i] = i;

int data[10];int i;

for(i=0; i<10; i++) data[i] = i;

data[0]data[1]data[2]data[3]data[4]

data[5]data[6]data[7]data[8]data[9]

data

int data[10];int i, *p;

for(i=0, p=data; i<10; i++){ *p = i; p++;}

int data[10];int i, *p;

for(i=0, p=data; i<10; i++){ *p = i; p++;}

0

1

234567

89

Pointer Arithmetic

Increment/decrement pointer (++ or –-) Add/substract an integer to a pointer (+ or += , - or -=) Pointers may be subtracted from each other

int data[10];int i;

for(i=0; i<10; i++) data[i] = i;

int data[10];int i;

for(i=0; i<10; i++) data[i] = i;

data[0]data[1]data[2]data[3]data[4]

data[5]data[6]data[7]data[8]data[9]

data

int data[10];int i, *p;

for(i=0, p=data; i<10; i++){ *p = i; p++;}

int data[10];int i, *p;

for(i=0, p=data; i<10; i++){ *p = i; p++;}

0

1

234567

89

Which one is performance more effective?Which one is performance more effective?

Pointer Arithmetic

Increment/decrement pointer (++ or –-) Add/substract an integer to a pointer (+ or += , - or -=) Pointers may be subtracted from each other

data[0]data[1]data[2]data[3]data[4]

data[5]data[6]data[7]data[8]data[9]

data

int data[10];int i, *p;

for(i=0, p=data; i<10; i++){ *p = i; p++;}

int data[10];int i, *p;

for(i=0, p=data; i<10; i++){ *p = i; p++;}

0

1

234567

89

int data[10];int i, *p;

for(i=0, p=data; i<10; i++) *p++ = i;

int data[10];int i, *p;

for(i=0, p=data; i<10; i++) *p++ = i;

Pointer Arithmetic

Increment/decrement pointer (++ or –-) Add/substract an integer to a pointer (+ or += , - or -=) Pointers may be subtracted from each other

data[0]data[1]data[2]data[3]data[4]

data[5]data[6]data[7]data[8]data[9]

data0

1

234567

89

int data[10];int i, *p;

for(i=0, p=data; i<10; i++) *p++ = i;

int data[10];int i, *p;

for(i=0, p=data; i<10; i++) *p++ = i;

int data[10];int i, *p;

for(i=0, p=data; i<10; *p++ = i++) ;

int data[10];int i, *p;

for(i=0, p=data; i<10; *p++ = i++) ;

範例:Insert/Delete Data into/from Arrays

void insertAt(int array[], int size, int at, int val);void deleteAt(int array[], int size, int at);

0 1 2 3 4 5 6 7 8 9data

insertAt(data, 10, 3, 5);

0 1 2 5 3 4 5 6 7 8data

deleteAt(data, 10, 5);

0 1 2 5 3 5 6 7 8 8data

範例:Insert/Delete Data into/from Arrays/* arrayOp.c */

/* insertAt: insert an element into an int array */void insertAt(int array[], int size, int at, int val){ int *p, *q; p = &array[at]; q = &array[size-1]; while(q > p){ *q = *(q-1); q--; } *p = val;}

/* deleteAt: delete an element from an int array */void deleteAt(int array[], int size, int at){ int *p, *q; p = &array[at]; q = &array[size-1]; while(p < q){ *p = *(p+1); p++; }}

/* arrayOp.c */

/* insertAt: insert an element into an int array */void insertAt(int array[], int size, int at, int val){ int *p, *q; p = &array[at]; q = &array[size-1]; while(q > p){ *q = *(q-1); q--; } *p = val;}

/* deleteAt: delete an element from an int array */void deleteAt(int array[], int size, int at){ int *p, *q; p = &array[at]; q = &array[size-1]; while(p < q){ *p = *(p+1); p++; }}

0 1 2 3 4 5 6 7 8 9data 0 1 2 3 4 5 6 7 8 9data

insertAt(data, 10, 3, 5);

0 1 2 5 3 4 5 6 7 8data 0 1 2 5 3 4 5 6 7 8data

deleteAt(data, 10, 5);

0 1 2 5 3 5 6 7 8 8data 0 1 2 5 3 5 6 7 8 8data

範例:Insert/Delete Data into/from Arrays

0 1 2 3 4 5 6 7 8 9data 0 1 2 3 4 5 6 7 8 9data

insertAt(data, 10, 3, 5);

0 1 2 5 3 4 5 6 7 8data 0 1 2 5 3 4 5 6 7 8data

deleteAt(data, 10, 5);

0 1 2 5 3 5 6 7 8 8data 0 1 2 5 3 5 6 7 8 8data

/* main.c */void insertAt(int array[], int size, int at, int val);void deleteAt(int array[], int size, int at);void listElements(int array[], int size);

main(){ int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

printf("Origin data:\n"); listElements(data, 10);

insertAt(data, 10, 3, 5); printf("After calling insertAt(data, 10, 3, 5):\n"); listElements(data, 10);

deleteAt(data, 10, 5); printf("After calling deleteAt(data, 10, 5):\n"); listElements(data, 10);}

/* main.c */void insertAt(int array[], int size, int at, int val);void deleteAt(int array[], int size, int at);void listElements(int array[], int size);

main(){ int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

printf("Origin data:\n"); listElements(data, 10);

insertAt(data, 10, 3, 5); printf("After calling insertAt(data, 10, 3, 5):\n"); listElements(data, 10);

deleteAt(data, 10, 5); printf("After calling deleteAt(data, 10, 5):\n"); listElements(data, 10);}

範例: Pointer Arithmetic#include <stdio.h>

main(){ int data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int *p, *q;

printf("data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n\n");

p = q = data; printf("p = q = data => p = q = %p\n", data); printf("q - p = %d\n\n", q - p);

p++; q += 5; printf("p++; q += 5 => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);

p -= 3; q--; printf("p -= 3; q-- => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);}

#include <stdio.h>

main(){ int data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int *p, *q;

printf("data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n\n");

p = q = data; printf("p = q = data => p = q = %p\n", data); printf("q - p = %d\n\n", q - p);

p++; q += 5; printf("p++; q += 5 => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);

p -= 3; q--; printf("p -= 3; q-- => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);}

data[0]data[1]data[2]data[3]data[4]

data[5]data[6]data[7]data[8]data[9]

data0

123

456789

範例: Pointer Arithmetic#include <stdio.h>

main(){ int data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int *p, *q;

printf("data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n\n");

p = q = data; printf("p = q = data => p = q = %p\n", data); printf("q - p = %d\n\n", q - p);

p++; q += 5; printf("p++; q += 5 => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);

p -= 3; q--; printf("p -= 3; q-- => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);}

#include <stdio.h>

main(){ int data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int *p, *q;

printf("data[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n\n");

p = q = data; printf("p = q = data => p = q = %p\n", data); printf("q - p = %d\n\n", q - p);

p++; q += 5; printf("p++; q += 5 => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);

p -= 3; q--; printf("p -= 3; q-- => p = %p, q = %p\n", p, q); printf("q - p = %d\n\n", q - p);}

data[0]data[1]data[2]data[3]data[4]

data[5]data[6]data[7]data[8]data[9]

data0

123

456789

Pointer Comparison <, <=, ==, >=, >

– Usually, they apply to pointers of array’s element– p q is true iff p - q 0 is true, e.g.,

p < q is true iff p – q < 0 is truep > q is true iff p – q > 0 is true

NULL == p Test the validity of p, e.g.,

#define NULL 0int *p = NULL;/* p may point to somewhere in the following */. . . . . . . . . . . . . . .if (NULL==p) printf("p is an invalid pointer");else printf("%d\n", *p);

Pointer Casting Pointers of the same type can be assigned to each ot

her– If not the same type, a cast operator must be used

Example:

main(){ int m=5; int *p, *q; char *r;

p = &m; /* ok */ q = p; /* ok */ r = p; /* warning */}

main(){ int m=5; int *p, *q; char *r;

p = &m; /* ok */ q = p; /* ok */ r = p; /* warning */}

Pointer Casting Pointers of the same type can be assigned to each ot

her– If not the same type, a cast operator must be used

Example:

main(){ int m=5; int *p, *q; char *r;

p = &m; /* ok */ q = p; /* ok */ r = (char *) p; /* ok */}

main(){ int m=5; int *p, *q; char *r;

p = &m; /* ok */ q = p; /* ok */ r = (char *) p; /* ok */}

範例: Pointer Casting

main(){ int m; int *p, *q; char *r;

m = 0x1ff; /* 511 */ printf("integer m = %d\n\n", m);

p = &m; /* ok */ q = p; /* ok */ r = (char *) p; /* ok */

printf("p, q, r all point to m:\n"); printf("p=%p, q=%p, r=%p\n", p, q, r); printf("*p=%d, *q=%d, *r=%d\n\n", *p, *q, *r);

*r = '\0';

printf("After setting the 1st byte of m zero, then\n"); printf("*p=%d, *q=%d, *r=%d\n", *p, *q, *r);}

main(){ int m; int *p, *q; char *r;

m = 0x1ff; /* 511 */ printf("integer m = %d\n\n", m);

p = &m; /* ok */ q = p; /* ok */ r = (char *) p; /* ok */

printf("p, q, r all point to m:\n"); printf("p=%p, q=%p, r=%p\n", p, q, r); printf("*p=%d, *q=%d, *r=%d\n\n", *p, *q, *r);

*r = '\0';

printf("After setting the 1st byte of m zero, then\n"); printf("*p=%d, *q=%d, *r=%d\n", *p, *q, *r);}

m

p

q

r

000001ff 00

範例: Pointer Casting

main(){ int m; int *p, *q; char *r;

m = 0x1ff; /* 511 */ printf("integer m = %d\n\n", m);

p = &m; /* ok */ q = p; /* ok */ r = (char *) p; /* ok */

printf("p, q, r all point to m:\n"); printf("p=%p, q=%p, r=%p\n", p, q, r); printf("*p=%d, *q=%d, *r=%d\n\n", *p, *q, *r);

*r = '\0';

printf("After setting the 1st byte of m zero, then\n"); printf("*p=%d, *q=%d, *r=%d\n", *p, *q, *r);}

main(){ int m; int *p, *q; char *r;

m = 0x1ff; /* 511 */ printf("integer m = %d\n\n", m);

p = &m; /* ok */ q = p; /* ok */ r = (char *) p; /* ok */

printf("p, q, r all point to m:\n"); printf("p=%p, q=%p, r=%p\n", p, q, r); printf("*p=%d, *q=%d, *r=%d\n\n", *p, *q, *r);

*r = '\0';

printf("After setting the 1st byte of m zero, then\n"); printf("*p=%d, *q=%d, *r=%d\n", *p, *q, *r);}

m

p

q

r

000001ff 00

Non-Typed Pointer void *

Generic pointer, represents any type No casting needed to convert a pointer to void

pointer void pointers cannot be dereferenced, e.g.,

int m=5, n;char c='a', d;void *p;

p = &m; /* ok */n = *p; /* invalid */

p = &c; /* ok */d = *p; /* invalid */

int m=5, n;char c='a', d;void *p;

p = &m; /* ok */n = *p; /* invalid */

p = &c; /* ok */d = *p; /* invalid */

int m=5, n;char c='a', d;void *p;

p = &m; /* ok */n = *(int *) p; /* ok */

p = &c; /* ok */d = *(char *) p; /* ok */

int m=5, n;char c='a', d;void *p;

p = &m; /* ok */n = *(int *) p; /* ok */

p = &c; /* ok */d = *(char *) p; /* ok */

C Program DesignC Pointers

Pointers and Arrays

Pointers vs. Arrays

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting operations

範例: Pointers vs. Arrays (I)

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++) printf("data[%d]=%d\tp[%d]=%d\t\t*(p+%d)=%d\n", i, data[i], i, p[i], i, *(p+i));}

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++) printf("data[%d]=%d\tp[%d]=%d\t\t*(p+%d)=%d\n", i, data[i], i, p[i], i, *(p+i));}

p == &data[0]

pointer-offset notation

data[i] == p[i]data[i] == *(p+i)

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

範例: Pointers vs. Arrays (II)

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++, p++) printf("data[%d]=%d\tp=%p\t*p=%d\n", i, data[i], p, *p);}

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++, p++) printf("data[%d]=%d\tp=%p\t*p=%d\n", i, data[i], p, *p);}

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

範例: Pointers vs. Arrays (III)

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++) printf("data[%d]=%d\tp=%p\t*p++=%d\n", i, data[i], p, *p++);}

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++) printf("data[%d]=%d\tp=%p\t*p++=%d\n", i, data[i], p, *p++);}

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

範例: Pointers vs. Arrays (IV)

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++, data++, p++) printf("*data=%d\t*p=%d\n",*data, *p);}

#include <stdio.h>

main(){ int data[]={2, -1, 60, 90, 35}; int *p, i;

p = data; /* or p = &data[0] */ printf("p = &data[0]\n"); for(i=0; i<5; i++, data++, p++) printf("*data=%d\t*p=%d\n",*data, *p);}

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

Arrays and pointers closely related– Array name like a constant pointer– Pointers can do array subscripting

operations

22

-1-1

6060

9090

3535

data[0]data

data[1]data[2]data[3]data[4]

p

範例: String Copy (I)#include <stdio.h>

void stringCopy(char* dest, const char* source);

main(){ char string1[10]; /* create a string buffer of size 10 */ char *string2="Hello"; /* create a string using pointer notation */ char string3[10]; /* create a string buffer of size 10 */ char string4[]="Good Bye"; /* create a string using array notation */

stringCopy(string1, string2); printf("string1=%s\n", string1);

stringCopy(string3, string4); printf("string3=%s\n", string3);}

void stringCopy(char* dest, const char* source){ while(*source!='\0'){ *dest = *source; dest++; source++; } *dest='\0';}

#include <stdio.h>

void stringCopy(char* dest, const char* source);

main(){ char string1[10]; /* create a string buffer of size 10 */ char *string2="Hello"; /* create a string using pointer notation */ char string3[10]; /* create a string buffer of size 10 */ char string4[]="Good Bye"; /* create a string using array notation */

stringCopy(string1, string2); printf("string1=%s\n", string1);

stringCopy(string3, string4); printf("string3=%s\n", string3);}

void stringCopy(char* dest, const char* source){ while(*source!='\0'){ *dest = *source; dest++; source++; } *dest='\0';}

範例: String Copy (II)#include <stdio.h>

void stringCopy(char* dest, const char* source);

main(){ char string1[10]; /* create a string buffer of size 10 */ char *string2="Hello"; /* create a string using pointer notation */ char string3[10]; /* create a string buffer of size 10 */ char string4[]="Good Bye"; /* create a string using array notation */

stringCopy(string1, string2); printf("string1=%s\n", string1);

stringCopy(string3, string4); printf("string3=%s\n", string3);}

void stringCopy(char* dest, const char* source){ while(*source) *dest++ = *source++; *dest='\0';}

#include <stdio.h>

void stringCopy(char* dest, const char* source);

main(){ char string1[10]; /* create a string buffer of size 10 */ char *string2="Hello"; /* create a string using pointer notation */ char string3[10]; /* create a string buffer of size 10 */ char string4[]="Good Bye"; /* create a string using array notation */

stringCopy(string1, string2); printf("string1=%s\n", string1);

stringCopy(string3, string4); printf("string3=%s\n", string3);}

void stringCopy(char* dest, const char* source){ while(*source) *dest++ = *source++; *dest='\0';}

範例: String Copy (III)#include <stdio.h>

void stringCopy(char* dest, const char* source);

main(){ char string1[10]; /* create a string buffer of size 10 */ char *string2="Hello"; /* create a string using pointer notation */ char string3[10]; /* create a string buffer of size 10 */ char string4[]="Good Bye"; /* create a string using array notation */

stringCopy(string1, string2); printf("string1=%s\n", string1);

stringCopy(string3, string4); printf("string3=%s\n", string3);}

void stringCopy(char* dest, const char* source){ while(*dest++ = *source++);}

#include <stdio.h>

void stringCopy(char* dest, const char* source);

main(){ char string1[10]; /* create a string buffer of size 10 */ char *string2="Hello"; /* create a string using pointer notation */ char string3[10]; /* create a string buffer of size 10 */ char string4[]="Good Bye"; /* create a string using array notation */

stringCopy(string1, string2); printf("string1=%s\n", string1);

stringCopy(string3, string4); printf("string3=%s\n", string3);}

void stringCopy(char* dest, const char* source){ while(*dest++ = *source++);}

strcpy

範例 :String Length

/* non-recursive version */int stringLength(const char *s){ int len=0;

while(*s++) len++; return len;}

/* recursive version */int recursiveStringLength(const char *s){ if(*s=='\0') return 0; return RecursiveStringLength(++s) + 1;}

/* non-recursive version */int stringLength(const char *s){ int len=0;

while(*s++) len++; return len;}

/* recursive version */int recursiveStringLength(const char *s){ if(*s=='\0') return 0; return RecursiveStringLength(++s) + 1;}

範例 :atoi(recurive version)

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); else if(str[start]=='\0') return val; else return recursive_atoi(str, val * 10 + (str[start] - '0'), start+1);}

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); else if(str[start]=='\0') return val; else return recursive_atoi(str, val * 10 + (str[start] - '0'), start+1);}

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0, 0);}

str

'1' '2' '5' '9' '3' '0' '1' '9' '\0'

leading_val=125

範例 :atoi(recurive version)

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); else if(str[start]=='\0') return val; else return recursive_atoi(str, val * 10 + (str[start] - '0'), start+1);}

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); else if(str[start]=='\0') return val; else return recursive_atoi(str, val * 10 + (str[start] - '0'), start+1);}

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

str

'1' '2' '5' '9' '3' '0' '1' '9' '\0'

leading_val=0

範例 :atoi(recurive version)

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); else if(str[start]=='\0') return val; else return recursive_atoi(str, val * 10 + (str[start] - '0'), start+1);}

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); else if(str[start]=='\0') return val; else return recursive_atoi(str, val * 10 + (str[start] - '0'), start+1);}

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

str

'1' '2' '5' '9' '3' '0' '1' '9' '\0'

leading_val=12593019 answer

範例 :atoi(recurive version)

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); if(*str =='\0') return leading_val; else return recursive_atoi(++str, leading_val * 10 + (*str - '0')); }

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char str[]){ return recursive_atoi(str, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(str[start] == '-') return -recursive_atoi(str, val, start+1); else if(str[start] == '+') return recursive_atoi(str, val, start+1); if(*str =='\0') return leading_val; else return recursive_atoi(++str, leading_val * 10 + (*str - '0')); }

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char str[]){ return recursive_atoi(str, 0);}

strstr

'1' '2' '5' '9' '3' '0' '1' '9' '\0''1' '2' '5' '9' '3' '0' '1' '9' '\0'

leading_val=125

strstr

'1' '2' '5' '9' '3' '0' '1' '9' '\0'

leading_val=12593019 answer

範例 :atoi(recurive version)

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(*str == '-') return -recursive_atoi(++str, leading_val); else if(*str == '+') return recursive_atoi(++str, leading_val); else if(*str =='\0') return leading_val; else return recursive_atoi(++str, leading_val * 10 + (*str - '0')); }

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(*str == '-') return -recursive_atoi(++str, leading_val); else if(*str == '+') return recursive_atoi(++str, leading_val); else if(*str =='\0') return leading_val; else return recursive_atoi(++str, leading_val * 10 + (*str - '0')); }

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(*str == '-') return -recursive_atoi(++str, leading_val); else if(*str == '+') return recursive_atoi(++str, leading_val); else if(*str =='\0') return leading_val; else return recursive_atoi(++str, leading_val * 10 + (*str - '0')); }

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

// RecursiveAtoi.c

// recursive_atoi: convert a decimal string (str)// into an integer with leading value (leading_val)int recursive_atoi(const char *str, int leading_val){ if(*str == '-') return -recursive_atoi(++str, leading_val); else if(*str == '+') return recursive_atoi(++str, leading_val); else if(*str =='\0') return leading_val; else return recursive_atoi(++str, leading_val * 10 + (*str - '0')); }

// atoi: convert a decimal string str into an integer// assume str is error freeint atoi(const char *str){ return recursive_atoi(str, 0);}

範例 :atoi(recurive version)

// main.c#include <stdio.h>

int atoi(char[]);

main(){ char str[20]; printf("Enter a numeric string:"); scanf("%s", str);

printf("The value you type is:%d\n", atoi(str));}

// main.c#include <stdio.h>

int atoi(char[]);

main(){ char str[20]; printf("Enter a numeric string:"); scanf("%s", str);

printf("The value you type is:%d\n", atoi(str));}

C Program DesignC Pointers

Using the const

Qualifier with Pointers

const Qualifier const qualifier

– Variable cannot be changed– Use const if function does not need to change a variable– Attempting to change a const variable produces an error

const pointers– Point to a constant memory location– Must be initialized when defined– int *const myPtr = &x;

Type int *const constant pointer to an int– const int *const Ptr = &x;

const pointer to a const int x itself can be changed, but not *Ptr

– const int *myPtr = &x; Modifiable pointer to a const int

範例 :#include <stdio.h>#include <ctype.h>

void convertToUppercase( char *sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase( char *sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

#include <stdio.h>#include <ctype.h>

void convertToUppercase( char *sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase( char *sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

Both sPtr and *sPtr are modifiable

Both sPtr and *sPtr are modifiable

範例 :#include <stdio.h>#include <ctype.h>

void convertToUppercase( char *sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase( char *sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

#include <stdio.h>#include <ctype.h>

void convertToUppercase( char *sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase( char *sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

範例 :#include <stdio.h>#include <ctype.h>

void convertToUppercase( const char *sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase( const char *sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

#include <stdio.h>#include <ctype.h>

void convertToUppercase( const char *sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase( const char *sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

sPtr is modifiable, while *sPtr is not

sPtr is modifiable, while *sPtr is not

範例 :#include <stdio.h>#include <ctype.h>

void convertToUppercase(char * const sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase(char * const sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

#include <stdio.h>#include <ctype.h>

void convertToUppercase(char * const sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase(char * const sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

*sPtr is modifiable, while sPtr is not

*sPtr is modifiable, while sPtr is not

範例 :#include <stdio.h>#include <ctype.h>

void convertToUppercase(char * const sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase(char * const sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

#include <stdio.h>#include <ctype.h>

void convertToUppercase(char * const sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase(char * const sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

void convertToUppercase( char * const sPtr ){ int i=0;

while (sPtr[i]){ if(islower(*sPtr)) sPtr[i] = toupper( sPtr[i] ); i++; }}

void convertToUppercase( char * const sPtr ){ int i=0;

while (sPtr[i]){ if(islower(*sPtr)) sPtr[i] = toupper( sPtr[i] ); i++; }}

範例 :#include <stdio.h>#include <ctype.h>

void convertToUppercase(const char * const sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase(const char * const sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

#include <stdio.h>#include <ctype.h>

void convertToUppercase(const char * const sPtr );

main(){ char string[] = "characters and $32.98";

printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); }

/* convert string to uppercase letters */void convertToUppercase(const char * const sPtr ){ while (*sPtr){ if(islower(*sPtr)) *sPtr = toupper( *sPtr ); ++sPtr; }}

Boths sPtr and *sPtr are not modifiable.

Boths sPtr and *sPtr are not modifiable.

C Program DesignC Pointers

Arrays of Pointers

Arrays of Pointers

Arrays can contain pointers For example: an array of strings

– char *suit[ 4 ] = {

"Hearts", "Diamonds", "Clubs", "Spades“

};– Strings are pointers to the first character– char * each element of suit is a pointer to a char– The strings are not actually stored in the array suit, only

pointers to the strings are stored

Arrays of Pointers

Arrays can contain pointers For example: an array of strings

– char *suit[ 4 ] = {

"Hearts", "Diamonds", "Clubs", "Spades“

};– Strings are pointers to the first character– char * each element of suit is a pointer to a char– The strings are not actually stored in the array suit, only

pointers to the strings are stored

範例 :/* playcard.c */#include <stdio.h>

void showCard(int card){ static const char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades“ }; static const char *face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

printf("%s of %s", card % 52, card % 13);}

void shuffle(int deck[]){ int i, n, t;

for(i=52; i > 1; i--){ n = rand() % i; t = deck[n]; deck[n] = deck[i-1]; deck[i-1] = t; }}

/* playcard.c */#include <stdio.h>

void showCard(int card){ static const char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades“ }; static const char *face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

printf("%s of %s", card % 52, card % 13);}

void shuffle(int deck[]){ int i, n, t;

for(i=52; i > 1; i--){ n = rand() % i; t = deck[n]; deck[n] = deck[i-1]; deck[i-1] = t; }}

範例 :

void shuffle(int deck[]){ int i, n, t;

for(i=52; i > 1; i--){ n = rand() % i; t = deck[n]; deck[n] = deck[i-1]; deck[i-1] = t; }}

void shuffle(int deck[]){ int i, n, t;

for(i=52; i > 1; i--){ n = rand() % i; t = deck[n]; deck[n] = deck[i-1]; deck[i-1] = t; }}

Recommended