# 指標

(1)

## 指標

(2)

(3)

(4)

(5)

(6)

int *int_ptr;

char *ch_ptr;

### 

float *float_ptr;

### 

double *double_ptr;

(7)

### 1008 1012

ptr是一個指向整數型 態的指標, 內容為該整 數的位址

i是一個整數, 內容為3, 與

*ptr的值一樣

(8)

&變數名稱

int i=3;

int *ptr;

ptr=&i;

### 整數指標

*ptr取得該指 標的內容值

&i取得該整數在記 憶體中 的位址

i是一個整數

(9)

*指標變數名稱

int i;

int *p;

p=&i;

*p=50

(10)

(11)

###  差值運算

int x = 10;

int *x = (int *)10;

x

*x

&x

x

(12)

例子

int *p,*q;

p=p+q; /* 不合法 */

(13)

(14)

### 

兩個相同資料型態的指標變數可以做減法運算

例子

(15)

call by value

### 

call by address

(16)

1 /* Fig. 7.6: fig07_06.c

2 Cube a variable using call-by-value */

3 #include <stdio.h>

4

5 int cubeByValue( int n ); /* prototype */

6

7 int main() 8 {

9 int number = 5; /* initialize number */

10

11 printf( "The original value of number is %d", number );

12

13 /* pass number by value to cubeByValue */

14 number = cubeByValue( number );

15

16 printf( "\nThe new value of number is %d\n", number );

17

18 return 0; /* indicates successful termination */

19

20 } /* end main */

21

22 /* calculate and return cube of integer argument */

23 int cubeByValue( int n ) 24 { 25 return n * n * n; /* cube local variable n and return result */

26 27 } /* end function cubeByValue */

(17)

### Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved.

(18)

int main() {

int number = 5;

number=cubeByValue(number);

}

int cubeByValue( int n ) {

return n * n * n;

} number

5

n

main

cubeByValue :

cubeByValue

### receives the call:

int main() {

int number = 5;

number = cubeByValue( number );

}

int cubeByValue( int n ) {

return n * n * n;

} number

5

n

5

cubeByValue

n

### and before

125

int cubeByValue( int n ) {

return n * n * n;

} int main()

{

int number = 5;

number = cubeByValue( number );

}

n cubeByValue

main :

5 number

5

### Analysis of a typical call-by-value. (Part 1 of 2.)

(19)

125 int main()

{

int number = 5;

number = cubeByValue( number );

}

int cubeByValue( int n ) {

return n * n * n;

} number

5

n

cubeByValue

main

### and before assigning the result to number

:

undefined

125 125

int main() {

int number = 5;

number = cubeByValue( number );

}

int cubeByValue( int n ) {

return n * n * n;

} number

125

n

main

number:

undefined

### Analysis of a typical call-by-value. (Part 2 of 2.)

(20)

1 /* Fig. 7.7: fig07_07.c

2 Cube a variable using call-by-reference with a pointer argument */

3

4 #include <stdio.h>

5

6 void cubeByReference( int *nPtr ); /* prototype */

7

8 int main() 9 {

10 int number = 5; /* initialize number */

11

12 printf( "The original value of number is %d", number );

13

14 /* pass address of number to cubeByReference */

15 cubeByReference( &number );

16

17 printf( "\nThe new value of number is %d\n", number );

18

19 return 0; /* indicates successful termination */

20

21 } /* end main */

22

23 /* calculate cube of *nPtr; modifies variable number in main */

24 void cubeByReference( int *nPtr ) 25 { 26 *nPtr = *nPtr * *nPtr * *nPtr; /* cube *nPtr */

27 } /* end function cubeByReference */

(21)

### Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved.

(22)

void cubeByReference( int *nPtr ) {

*nPtr = *nPtr * *nPtr * *nPtr;

} int main()

{

int number = 5;

cubeByReference( &number );

}

void cubeByReference( int *nPtr ) {

*nPtr = *nPtr * *nPtr * *nPtr;

}

int main() {

int number = 5;

cubeByReference( &number );

}

number

5

nPtr

number

5

nPtr

main

### calls

cubeByReference :

cubeByReference

*nPtr

### is cubed:

undefined

call establishes this pointer

125

void cubeByReference( int *nPtr ) {

*nPtr = *nPtr * *nPtr * *nPtr;

} int main()

{

int number = 5;

cubeByReference( &number );

}

number

125

nPtr

*nPtr

### is cubed and before program control returns to

main:

called function modifies caller’s variable

(23)

(24)

(25)

### 

int array[10]; 我們以陣列名為array代表陣列的起 始位址, 以array+1表示下一個位址.

### 

(26)

1 /* Fig. 7.20: fig07_20.cpp

2 Using subscripting and pointer notations with arrays */

3

4 #include <stdio.h>

5

6 int main() 7 {

8 int b[] = { 10, 20, 30, 40 }; /* initialize array b */

9 int *bPtr = b; /* set bPtr to point to array b */

10 int i; /* counter */

11 int offset; /* counter */

12

13 /* output array b using array subscript notation */

14 printf( "Array b printed with:\nArray subscript notation\n" );

15

16 /* loop through array b */

17 for ( i = 0; i < 4; i++ ) {

18 printf( "b[ %d ] = %d\n", i, b[ i ] );

19 } /* end for */

20

21 /* output array b using array name and pointer/offset notation */

22 printf( "\nPointer/offset notation where\n"

23 "the pointer is the array name\n" );

24

### Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved.

(27)

25 /* loop through array b */

26 for ( offset = 0; offset < 4; offset++ ) {

27 printf( "*( b + %d ) = %d\n", offset, *( b + offset ) );

28 } /* end for */

29

30 /* output array b using bPtr and array subscript notation */

31 printf( "\nPointer subscript notation\n" );

32

33 /* loop through array b */

34 for ( i = 0; i < 4; i++ ) {

35 printf( "bPtr[ %d ] = %d\n", i, bPtr[ i ] );

36 } /* end for */

37

38 /* output array b using bPtr and pointer/offset notation */

39 printf( "\nPointer/offset notation\n" );

40

41 /* loop through array b */

42 for ( offset = 0; offset < 4; offset++ ) {

43 printf( "*( bPtr + %d ) = %d\n", offset, *( bPtr + offset ) );

44 } /* end for */

45

46 return 0; /* indicates successful termination */

47

48 } /* end main */

(28)

## Program Output

(29)

(30)

###  例子

int array[3][4], *ptr;

ptr=(int *)array;

array[1][2] 跟 ptr[3*1+2]的比較

(*(array+1))[1]跟*((array+1)[1])的比較

例子

(31)

### 

int *int_ptr[10]; // 整數指標陣列

### 

float *float_ptr[10];

### 

double *double_ptr[10];

char *str[10];

(32)

### 

void (*func)(void);

(33)

ptr *ptr **ptr

(34)

(35)

### 

array[i][j] *(*(array+i)+j)

(36)

(37)

### 

 int main(int argc,char *argv[])

### 

 int main(int argc,char argv[][]) /* 使用二維字元陣列（字串陣列） */

### 

 int main(int argc,char **argv) /* 使用指標的指標 */

### 

argc表示命令列中參數字串的個數.

### 

argv表示指向命令列中所有參數字串的指標.

例子

(38)

### 

C語言不能宣告一個陣列大小為變數的陣列

(39)

### 

#include <stdlib.h>

### 

#include <malloc.h>

###  範例

char *ptr;

ptr = (char *)malloc(sizeof(char)*9);

(40)

Free(ptr);

(41)

Updating...

## References

Related subjects :