# pointer application

Post on 22-Feb-2016

23 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

Pointer Application. 指向二維陣列的指標. int z[4][2] 是一個二維陣列，也代表一個「 包含兩 個整數元素的陣列」的位 址 我們可以宣告一個指向「包含兩個整數元素的陣列」的指標，用來儲存這個 陣列的 位 址 int (* pz )[2 ]; pz 是一個指到整數陣列的 指標，它 所指到的陣列包含了兩個整數 元素 若不加括號 int * pz [2] 代表的是有 2 個 int * 的陣列 pz = z ; 把 pz 這個指標當作二維陣列來 使用 例如： pz [2 ][1] 就相當於是 z[2][1]. - PowerPoint PPT Presentation

TRANSCRIPT

Pointer Application

Pointer Applicationint z[4][2] int (*pz)[2];pz int *pz[2]2int*pz = z; pz pz[2][1] z[2][1]

functionint (*ap)[COLS]int arr2[ROWS][COLS] COLS [COLS]compiler COLS pa pb pa+1 pb+1

8-1()*(aPtr[0]) aPtr[0][0]*(aPtr[5]+2) aPtr[5][2]aPtr

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 498-2malloc() free()stdlib.hfunctionsmalloc() free() malloc()(bytes)void int* ptr = (int*)malloc(array_size* sizeof(int));free()free(ptr);

free() memory leak How many doubles do you want? 101: 0.5635853: 0.8087414: 0.5850097: 0.8959628: 0.8228409: 0.746605exit(EXIT_FAILURE)exit() function EXIT_FALURE stdlib.h 8-3

strlen(buffer)+1 bytes ('\0')/AbPeterJimyaaaaaaaaaaaaaaaaaaaaaaaa

8-4

Function pointerfunctionfunc1func2function pointerpointer:fun_ptrfun1 : 10fun_ptrfun2 : 48-58void*voidvoid **void(dereference)stdlib.hqsortqsort(sort)qsortqsortvoid qsort ( void * base, size_t num, size_t size, int ( * compar ) ( const void *, const void * ) ); sizeof int ( * compar ) ( const void *, const void * )qsortfunction(1,2)01212pointer const

function pointervoid*qsort

1 2 3 4 5 6 8-6strcmpstrcmpint strcmp ( const char* str1, const char* str2 );\00: 0: ascii codepointerstrcpychar*!

intqsortcompareint*char*qsortcomparechar*char**

char*addresschar**strcmp(a~z)zaqqAbccccaaaaaawwwwwAbcaaaaaacccwwwwwzaqq8-7RecursionRecursion()Recursionfunction(for,while)recursion Why use recursion? ?(direct recursion)function(indirect recursion)functionfunctionfunctionfunction(tail recursion)

1N

8-88-91(Cont.): !??linux

segmentation fault

Why segmentation fault?segmentationdata

DATA0x00016000x00016FF0x00018000x00019FF0x00020000x00020FF?2fibonacci

8-108-113list[]=abc,i=0,n=2:

8-12-3nABC-(Cont.)ABC-(Cont.)?Step1:ABStep2:ACStep3:BC-(Cont.)Step1:n-1ABStep2:ACStep3:n-1BC

step1step2step38-13Modular Programming

main.chanoi.chanoi.h8-148-138-138-13

hanoifunctionhanoi.hmain.cinclude hanoi.h

hanoi.h(1)compilerinclude#endif

main.chanoi.chanoi.h8-14codecode::blocksprojectprojectWindowslinuxOS

gcc main.c hanoi.c hanoi.h1 C (header file) #ifndef #define #endif (include) C #pragma once http://ccckmit.wikidot.com/cp:includeguardOS(flags)-Dx86_64x86_64flags-Ddefine

Code::blocksappendix8-15gcc 8-15.c DARM