Title: Asst.Prof.Urachart Kokaew
1????? 9 ?????????????????????????????????????????
Asst.Prof.Urachart Kokaew
2?????????? (Pointer)
- ?????????? ??? ??????????????????????????????????
???? ?????????????????????? machine
????????????????????????? type ??????????????????
??????????????????????? indirectly - ???????????????
-
-
type var
3?????????? (Pointer) (???)
- ???? int iptr //?????????????????????????????
?? integer -
- char cptr //???????????????????????????????
character
iptr integer var
cptr char var
4????????????
- ???????? ????????????????????????? 2
?????????????????????????? ?????? - ??????? indirect operator
- ?????????????????????????????????????????????????
??????????????????????????????????????????????????
? - ??????? address operator
- ??????????????????????????????????????????????
????????????????????????????
5???????????? (???)
- ???????? ????
- char ptr NULL
- ???? declaration ??????????????????????????????
????????? ptr ??????????? NULL - ptr NULL
- ???? executable code ????????? NULL
???????????????????????? ptr ?????
6???????? 1. ???????????????????????
- main()
-
- int num 3
- int ptr
- ptr num
- printf(The value of num is d.
- Its address is lu.,ptr,(long)ptr)
7??????????????????
- ???????
- gtThe value of num is 3. Its address is
448925078. - ???????? ?????????????????????????????????????????
?????????????????? -
8???????????????
Declaration int num 3 int ptr num int var ptr
Executable code ptr num num ptr
3
3
9???????? 2 ???????????????????????
- main()
-
- static int number6 1,2,3,4,5,6
- int i,ptr
- for(i0ilt6i)
- ptr numberi
- printf(d\n,ptr)
-
10?????????????????????? 2
???????? ???????????? int ptr???????????? ptr
?????????????????????????
ptr
number ???? ptr number0
11????????????? 2
number
i 0 i 1 i 2 i 3 i 4 i 5
ptr
0 1 2 3 4 5
1
2
3
4
5
6
ptr ptr ptr ptr ptr
12???????? 3 ?????????????????????????????
- int strlen(s)
- char s
-
- char p s
- while(p ! \0)
- p
- return (p-s)
13???????????????????????? 3
- ??????????????????????????????????? string
- ??? string ???????????????????????????????????????
???????? string ??????????????????????????????????
A B C D E F \0
S
P
?????????????? P-S
14?????????????????????
- ???????? ????????????????????????????????????????
?????????????????????????????????????????????
???? int parray10 -
parray0 parray1 parray2 . . parray9
parray0 parray1 parray2 . . parray9
parray0 parray1 parray2 . . parray9
parray0 parray1 parray2 . . parray9
parray0 parray1 parray2 . . parray9
int.var int.var int.var int.var
15???????? 4 ????????????????????????????
- main()
-
- int j,n11n22n33ptr,parray4
- parray1 n1
- parray2 n2
- ptr n3
- parray3 ptr
- for(j1jlt4j)
- printf(d\n,parrayj)
16????????????????????????? 4
- ?????????????????????????????????????????? int
var - ??????????????????
- 1
- 2
- 3
17???????????????
- declaration
- int n11,n22,n33,ptr, parray4
n1 n2 n3
n1 n2 n3
1 2 3
1 2 3
parray
parray
0 1 2 3 ptr
0 1 2 3 ptr
18???????????????????????????????
- ???????????????????????????????????????????
????????????????????????? string
?????????????????????? ??????????? string
???????????????????????????????? - ??????????????????????????????????????????????????
?????????? ??????????????????????????????
19???????? 5 ???????????????????????????????
- char month(n)
- int n
- static char name
- illegal month,January,
February,March,April,May,June,July,
August,September, October,November,
December - return((nlt1 ngt12)?name0 namen)
-
- char month()
- main()
- int num
- puts(Enter number of month)
- scanf(d,num)
- printf(The name of month is s,month(num))
20????????????????????????? 5
- ???????
- Enter number of month
- gt 6
- The name of month is June
21???????????????
name0 name1 name2 name12 illegal month
name0 name1 name2 name12 January
name0 name1 name2 name12 February
name0 name1 name2 name12 . .
name0 name1 name2 name12 December
22Content ??SHEET
- 9.1 ?????????????? Pointer
- 9.2 ????????????? Pointer
- 9.3 ??????????????? Pointer ??? Array
- 9.4 ??????????????? Pointer ??? String
- 9.5 Array ??? Pointer
- 9.6 Pointer ??? Pointer
- 9.7 Pointer ??? ?????????? Structure
239.1 ?????????????? Pointer
- ?????????????? Pointer ???????????????????????????
?????? ????????????????????????? ??????????????
?????? - int pt
- ???????????????? pt ?????????? Pointer ????????
Address ????????????????????????????
24????????????? pointer
- ?????????????????????? 2 ?????? pointer ???
- ??????????? ???????? ????????? ??????????????
Address (Address ??????? ???????????? Pointer
??????????????????????? ) - ??????????? ???????? Address ???????????????????
????????????? ???????????
25??????????????????? pointer
- ?????????????????? 2
- ?????????????????? pointer ???????????????????????
????????? - ?????????????????? 3
- "Warning c\prg\Prg08-03.cppSuspicious pointer
Conversion in function main - C Error pointer1.cpp(28) E2034 Cannot
convert 'int ' to 'float ' - ?????????????????? 4
- Null pointer assignment
269.2 ????????????? Pointer
?????? ,,- ??? -- ??????????????????
???????????? ???? ?? ????????? Pointer ??????????
???????????????????? ???? pt pt60
pt -- ??????????????????????????
Pointer ?????????????????????????? compiler
??????? byte ???????????????? ????
279.2 ????????????? Pointer
??.?????? compiler ???????? ???????????? a
??????? Address 40 ??????????? ptt
?????????????? Pointer pt ?????????? Address 42
(????? 2 Byte) ????????? a ????????????????????
Pointer pt ?????????? Address 44 (????? 4
Byte) (?????????????????????????????5-8)
289.3 ??????????????? Pointer ??? Array
- Pointer ???????????????????? Array
- (?????? Array ??????)
- ???????????????????? 9 ?????? pt number ???????
Pointer pt ?????????????????? Array number
?????????????????????? Array ?????????????????????
??? Address ???????? Pointer ?????????? Pointer
????? Address ?????????? ?????????????? - (????????????????????????????? 9-12)
299.4 ??????????????? Pointer ??? String
- (????????????????????????????? 13-17)
- ????????????????? 13
- ????????? 7 ???????? spt str
- ????? Pointer spt ?????????????????? Array
String str - ?????? for ?????????????????????????????????
Array ?? ???????????????? ????????????????????????
????? String null
309.5 Array ??? Pointer
- (????????????????????????????? 18-19)
- Array ??? Pointer ?????? Address ?????????
??????????????????? ?????????????? - Data type pointer namrsize
- ???????????? int iap5
- Array ??? Pointer iap ?????????? Pointer
5 ????????????? Address ??????????????????????????
?????? ??????????????????? Pointer ?????? 2 ????
Address ????????? ???????????????? x
????????????? iap2 x
31- ???????????? 18 ???????? pt ???? Array ???
Pointer ???????????? Pointer 3 ??? ??????????
Address ????????????????????????? ?????? Pointer
?????? Pointer ???????? ??????????????? ???
Address ??????????????????? Array x
??????????????????????????? Array x
???????????????? ?????? Array ????
329.6 Pointer ??? Pointer
-
- ???????????? 1 Pointer ??? Pointer
- ?????????? Pointer ??? Pointer
?????????????????????? ??? 1 ??? ???? Pointer
???? int pt - pt ?????????? Pointer ??????????? Address
????????? Pointer ????????? Pointer ?????????????
?????? Address ?????????????????????????
(????????????????????????????? 20-21)
339.7 Pointer ????????????? Structure
??????? ?????????????????? 22 Input data to
structure by pointer 3 Item Code 121 Name
AA Age 12 Code 122 Name BB Age 23 Code
123 Name CC Age 14 Display data from structure
by use pointer -gt 121 AA 12 ..