programming_in_ANSI_C-Chapter_11Pointer.ppt_第1頁
programming_in_ANSI_C-Chapter_11Pointer.ppt_第2頁
programming_in_ANSI_C-Chapter_11Pointer.ppt_第3頁
programming_in_ANSI_C-Chapter_11Pointer.ppt_第4頁
programming_in_ANSI_C-Chapter_11Pointer.ppt_第5頁
已閱讀5頁,還剩111頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

Chapter 11,Pointers,Outline,Introduction Pointer and Function Pointer and Array Dynamic allocation string, character array & pointer Examples,Introduction,內(nèi)存 Random Access Memory 地址 Address,計算機內(nèi)的存儲部件,所有指令和數(shù)據(jù)都保存在內(nèi)存中 速度快,但是掉電即失 可以隨機訪問 只要指名要訪問的內(nèi)存單元的地址,就可以立即訪問到該單元 地址是一個無符號整數(shù),其字長一般與主機相同 內(nèi)存中的每個字節(jié)都有唯一的一個地址 地址按字節(jié)編號,空間按類型分配,Variables & Address,Memory: int a=0; a 0x0037b000,0,變量p,0x0037b003,變量p中存放變量a的首地址,0x0037b003,How to store the address?,A pointer(指針) is a variable that contains the address of another variable.,the pointer of a variable(變量的指針) & the pointer variable(指針變量),指針是一種特殊的數(shù)據(jù)類型,用于存放地址型數(shù)據(jù) 變量的指針the pointer of a variable identical with 變量的地址 指針變量the pointer variable 是C語言中專門用于存放地址型數(shù)據(jù)的變量,the pointer of a variable & the pointer variable,int a=10,c=5; float b=3.5;,Memory allocation,a address,a value,pointer,a address,a -is int variable,store value i -is pointer, store address,address=pointer,4 bytes,Introduction,Direct and Indirect Addressing,How to r/w the data in memory? 通過變量的地址訪問變量所在的存儲單元 兩種尋址方式: Direct Addressing-直接尋址 按變量地址存取變量值 Indirect Addressing-間接尋址 通過存放變量地址的變量去訪問變量,Example: int a = 0; int *p = Direct access: &a Indirect access:*i,Pointer variables, simply called pointers Holding memory addresses as their values A kind of data type Normally, a variable contains a specific value, e.g., an integer, a floating-point value However, a pointer contains the memory address of a variable that in turn contains a specific value.,Short summary of Pointer,Introduction,definition of pointer variables,base-type * pointer-variable;,Where: base-type is the type of the value to which the pointer points pointer-variable is the variable being declared,Example:,int *p; Char *cptr; int *p1, *p2; float *pf;,Initialization,int a, b; int *p1 = ,Introduction,fundamental pointer operations,Two operators to manipulate pointer values: & Address-of * Value-pointed-to (dereferencing),Example:,int x,y; int *p1, *p2;,Introduction,x = -42; y = 163,p1 = ,Introduction,*p1 = 17;,p1 = p2;,Analyzing,*(&a) /*該表達(dá)式引用的是變量a的內(nèi)容*/ &(*p1) /*該表達(dá)式的值代表的是變量a的地址*/,int a, b; int *p1, *p2; p1 = ,Introduction,special pointer NULL,NULL is a constant 0,int *p; p = NULL,指針變量的特點:,指針變量也是一種變量,因此,它與其它類型變量有相同之處: 在內(nèi)存中都占據(jù)一定的存儲空間 必須遵循“先定義、后使用” 的規(guī)律,指針變量的特殊性 :,(1)指針變量的內(nèi)容只能是地址,而不能是數(shù)據(jù) int x, *p; float y; p = x; /*錯誤,指針p的內(nèi)容應(yīng)為地址*/ p = /*錯誤,必須用相同基類型的變量地址對指針賦值*/,p = /*正確,用相同基類型的變量的地址對指針 變量進行初始化 */,指針變量的特殊性 :,(2)指針變量必須經(jīng)初始化后才能使用 #include void main() int x, *p; x = 10; *p = x; printf(“%dn“,*p); ,指針變量必須 先賦值,再使用,指針變量的特殊性 :,(3)指針運算實質(zhì)上就是對地址的運算。因此,指針運算只能參與 賦值運算 算術(shù)運算 關(guān)系運算,指針運算賦值運算,指針在使用前一定要賦值 為指針變量賦的值必須是一個地址,void main() int *p; scanf(“%d“,p); ,void main() int a,*p= ,錯!,指針運算算術(shù)運算,加和減,即加、減一個整數(shù)或加1、減1 int *p; p+; /*指針p的指向 原指針值加sizeof(int)個字節(jié) */ 0x0037b003 0x0037b003+ sizeof(int),指針運算算術(shù)運算,pi p id (i為整型數(shù),d為p指向的變量所占字節(jié)數(shù) p+, p-, p+i, p-i, p+=i, p-=i 若p1與p2指向同一數(shù)組,p1-p2=兩指針間元素個數(shù)(p1-p2)/d p1+p2, p1*p2, p1/p2無意義,Examples:,int a10; int *p1=,指針運算算術(shù)運算,注意區(qū)別下面兩條語句: a = *p+; a = (*p)+;,a = *p; p = p + 1;,a = *p; *p = *p + 1;,指針運算關(guān)系運算,指向同一個數(shù)組中的不同元素的兩個指針可以進行各種關(guān)系運算 要求參與比較的兩個指針的基類型必須一致 兩個指針的關(guān)系運算表示它們所指向的變量在內(nèi)存中的前后位置關(guān)系。,指針運算關(guān)系運算,若p1和p2指向同一數(shù)組,則 p1p2 表示p1指的元素在后 p1= =p2 表示p1與p2指向同一元素 若p1與p2不指向同一數(shù)組,則比較無意義 指針不與非指針量進行比較,但可與NULL(即0值)進行等或不等的關(guān)系運算 判斷p是否為空指針p=NULL或p!=NULL,為什么引入指針的概念,指針為函數(shù)提供修改變量值的手段 指針可以改善某些子程序的效率 指針為C的動態(tài)內(nèi)存分配系統(tǒng)提供支持 指針為動態(tài)數(shù)據(jù)結(jié)構(gòu)提供支持 鏈表list 隊列queue 二叉樹binary-tree ,指針的使用原則,永遠(yuǎn)要清楚每個指針指向了哪里 地址值的有效性 永遠(yuǎn)要清楚指針指向的是什么 (間接)訪問的變量的正確性,int i,*p; p=,int *p; float *q; p=q;,int i; float *p; p=,int *p; p=100;,指針變量只 存放地址!,一個指針變量不能指向與其類型不同的變量!,我是真的, 你猜對了!,應(yīng)在類型相同的指針變量之間賦值,Which one is correct?,&與*,*用來取指針指向地址的內(nèi)容 int i, *p; p = ,指針的指向,指針指向非其定義時聲明的數(shù)據(jù)類型,將引起warning void*類型的指針可以指向任意類型的變量 指針在初始化時一般int *p=NULL; NULL表示空指針,即無效指針,不指向任何內(nèi)存單元 只是邏輯上無效,并不是真正地?zé)o效 如果指針指向一個非你控制的內(nèi)存空間,并對該空間進行訪問,將可能造成危險,指針變量與其它類型變量的對比,共性 在內(nèi)存中占據(jù)一定大小的存儲單元 先定義,后使用 特殊性 內(nèi)容只能是地址,而不能是數(shù)據(jù) 必須初始化后才能使用,否則指向不確定的存儲單元 只能指向同一基類型的變量 可參與有限的運算: 加/減某整數(shù),自增、自減、關(guān)系、賦值,Outline,Introduction Pointer and Function Pointer and Array Dynamic allocation string, character array & pointer Examples,既然指針是一種數(shù)據(jù)類型,自然可以做函數(shù)的參數(shù)argument和返回值的類型 指針做參數(shù)的經(jīng)典例子:兩數(shù)的互換 swap (argument 1, argument 2),Pointer and Function,void Swap(int *x,int *y) int temp; temp = *x; *x = *y; *y = temp; ,main() int a, b; a = 15; b = 8; Swap( ,void Swap(int x,int y) int temp; temp = x; x = y; y = temp; ,main() int a, b; a = 15; b = 8; Swap(a, b); printf(“a=%d,b=%d“,a,b); ,code 1,code 2,Swap two integers value,實 參,形 參,結(jié)果有何不同?,Not Work!Why?,pointer as arguments,主調(diào)函數(shù),被調(diào)函數(shù),void main() int a, b; a = 15; b = 8; Swap(a, b); printf(“a=%d,b=%d“,a,b); ,void Swap(int x, int y) int temp; temp = x; x = y; y = temp; ,15,15,a,b,實 參,形 參,8,8,程序 1,x,y,簡單變量作函數(shù)參數(shù),主調(diào)函數(shù),被調(diào)函數(shù),void main() int a, b; a = 15; b = 8; Swap( ,void Swap(int *x, int *y) int temp; temp = *x; *x = *y; *y = temp; ,&a,&a,實 參,形 參,&b,&b,程序 2,x,y,15,a,b,8,指針變量作函數(shù)參數(shù),swap函數(shù)的幾種錯誤形式(1/3),參數(shù)單向傳遞 void Swap(int x, int y) int temp; temp = x; /*x,y為局部變量*/ x = y; y = temp; ,void Swap(int *x, int *y) int temp; temp = *x; *x = *y; *y = temp; ,swap函數(shù)的幾種錯誤形式(2/3),參數(shù)單向傳遞 void Swap(int *p1, int *p2) int *p; p = p1; /*p1,p2為局部變量*/ p1 = p2; p2 = p; ,void Swap(int *x, int *y) int temp; temp = *x; *x = *y; *y = temp; ,swap函數(shù)的幾種錯誤形式(3/3),指針p沒有有效地址 void Swap(int *p1, int *p2) int *p; /*指針p未初始化*/ *p = *p1; *p1 = *p2; *p2 = *p; ,void Swap(int *x, int *y) int temp; temp = *x; *x = *y; *y = temp; ,Return char value: char min(char a10) char i,m; m=a0; for(i=1;iai) m=ai; return m; ,Return char * pointer char *min(char a10) char i,*m; m= ,base-type * function(parameters list),pointer as return value,Pointer and Function,call by reference to return multiple results,#define MinutesPerHour 60 void ConverTimetoHM(int time, int *pHours, int *pMinutes); void main( ) int time, hours,minutes; printf(“Test Program to convert time values.n); pintft(“Enter a time duration in minutes:”); scanf(“%d”,time); ConvertTimeToHM(time, ,Pointer and Function,void ConverTimetoHM(int time, int *pHours, int *pMinutes) *pHours = time/MinutesPerHour; *pMinutes = time%MinutesPerHour; ,Pointers to functions,int (*p)( ); p是指向int函數(shù)的指針變量 p=max; p是指向int型函數(shù)max的指針變量,function pointer:entrance address of a function(函數(shù)的入口地址-用函數(shù)名表示),Definition:,int max(int x, int y) return xy?x:y; void main( ) int c,a=15,b=12,(*p)( int,int ); p=max; /*p points to max()*/ c=(*p)(a,b);/*call function*/ printf(“max=%dn”,c); ,call a function using function pointer(1),c=max(a,b);,int sum(int a, int b) int res = a + b; return res; void main() int z; int (*fp)(int, int); fp = sum; z = (*fp)(3, 4); z = sum (3, 4); ,call a function using function pointer(2),Outline,Introduction Pointer and Function Pointer and Array Dynamic allocation string, character array & pointer Examples,Pointer & One-dimensional array Pointer & Two-dimensional array Row pointer & row address Column pointer & column addresses Array Pointer & Pointer Array A Pointer points to a pointer etc.,Pointer and Array,指針與一維數(shù)組,數(shù)組名就是一個常量指針 不能修改該指針的指向 指針也可當(dāng)作數(shù)組名使用 short *p, a10; p = a; 數(shù)組元素的幾種等價引用形式 ai *(a+i) pi *(p+i),a pointer and an array,int a10, *pa;,pa = ,pa+i,pa=,a+i,pa+i a+i &a0+i*m (m is the memory size for each element),*(pa+i) *(a+i) a i (indirect access the array member),m,pa+i,pa+1 points to the next element, and pa+i points to the i-th element next to pa.,The name of an array is the address of the initial element. a &a0,pa=a; pa=&a0,Pointer and Array,指針與一維數(shù)組 輸入輸出數(shù)組的全部元素,void main( ) int a10; int i; for (i=0; i10; i+) scanf(“%d“, ,void main( ) int a10; int *p, i; for (p=a; p(a+10); p+) scanf(“%d“, p); for (p=a; p(a+10); p+) printf(“%d “, *p); ,下標(biāo)法,指針法,Example:,Pointer and Array,void main( ) int a = 1,2,3,4,5 ; int i; for(i=0;i5;i+) printf(“%d ”,ai ); ,void main( ) int a =1,2,3,4,5 ; int i; for(i=0;i5;i+) printf(“%d ”, * ( a+ i ) ); ,void main( ) int a =1,2,3,4,5; int i; int *pa=a; for( i=0;i6;i+) printf(“%d”,*(pa+i); ,Example:,p,p-1,p-2,p-3,p+2,p+1,p+3,a,int a10,*p;,p= p points to ? a+?,a6,a5,a is constant,Pointer and Array,incrementing & decrementing pointers,Pointer and Array,#include void main( ) char s180, s280,*p1=s1,*p2=s2; gets(s1); /*enter s1 */ while(*p1!=0)/*copy s1 to s2*/ *p2 = *p1; p1+;p2+; /*points to next character*/ puts(s2); ,right?,More Pointers,int x = 1, y = 2, z10; int *ip; /* ip is a pointer to int */ ip = /* ip now points to z0 */,A Puzzle,int x = 1, y = 2, z10; int *ip; ip = ,A Puzzle,int x = 1, y = 2, z10; int *ip; ip = /* 3 3 3 */,Relationship between two pointers,int a10,*p=,When two pointers points to the same array,pq “false” p=q “false”,for(p=a,q=a+9;p=q;p+) printf(“%dn”,*p);,for(i=0,q=9;i=q;i+) printf(“%dn”,ai);,char a6=“china”,*p=,When two pointers points to the same array,p,p - a = 5, It is the elements number between p and a。i.e. “china” length.,a,Subtraction between two pointers,#include “stdio.h” void main( ) char s80; gets(s); printf(“%s length=%dn”, s, strlen(s) ); int strlen(char *s) /* get the length of string */ char *p; p=s; while(*p!=0) p+; /*p points to the end of string*/ return p-s; /*return length of string */ ,指針與二維數(shù)組,C將二維數(shù)組看作一維數(shù)組的嵌套,每個一維數(shù)組的 元素又是一個一維數(shù)組,a a0+0,a+1 a1+0,a0+1,a0+2,&a00,&a10,&a11,a1+1,&a12,&a01,&a02,a1+2,int a23;,指針與二維數(shù)組,e.g. 任意輸入英文的星期,查找星期表,輸出其對應(yīng)的數(shù)字。 char weekDay710 = “Sunday“,“Monday“, “Tuesday“,“Wednesday“,“Thursday“,“Friday“, “Saturday“;,星期表,#include void main() int i, pos; int findFlag = 0; char x10; char weekDay10 = “Sunday“,“Monday“,“Tuesday“, “Wednesday“,“Thursday“,“Friday“,“Saturday“; printf(“Please enter a string:“); scanf(“%s“, x); for (i=0; i7 ,第i行第0列的地址,指針與二維數(shù)組,a 代表二維數(shù)組的首地址,第0行的地址 a+i 代表第i行的地址 *(a+i) 即 ai 代表第i行第0列的地址 *(a+i)+j 即 ai+j 代表第i行第j列的地址 *(*(a+i)+j ) 即 aij 代表第i行第j列的元素,指針與二維數(shù)組,aij地址等價引用方式 &aij ai+j *(a+i)+j &(*(a+i)j,元素aij等價引用方式 aij *(ai+j) *(*(a+i)+j) (*(a+i)j,Column pointer 指針與二維數(shù)組列指針,二維數(shù)組a23的列指針 int *p1; p1 = *a;/用列地址初始化 相對于數(shù)組起始地址的偏移量:i*m+j for (i=0; in; i+) for (j=0; jm; j+) printf(“%d“,*(p1+i*m+j);,Row pointer 指針與二維數(shù)組行指針,二維數(shù)組a23的行指針 int (*p2)3; p2 = a; /用行地址初始化 for (i=0; in; i+) for (j=0; jm; j+) printf(“%d“,*(*(p2+i)+j);,e.g.求最高分及其所在班級和學(xué)號,void main() maxScore = FindMax(*score, CLASS, STUD, ,Pointer array 指針數(shù)組,元素均為指針類型數(shù)據(jù)的數(shù)組,稱為指針數(shù)組 定義形式為: 類型關(guān)鍵字 *數(shù)組名數(shù)組長度; 例如: char *pStr5;,base-type *array-nameSIZE,.;,int b3=11,12,13, *a3= Output:,11,12,13,Pointer array,字符串按字典順序排序 二維數(shù)組,char strN10 = “Pascal“,“Basic“,“Fortran“, “Java“,“Visual C“; char temp10=0; / for (i=0; iN-1; i+) for (j = i+1; jN; j+) if (strcmp(strj, stri) 0) strcpy(temp,stri); strcpy(stri,strj); strcpy(strj,temp); ,Memory layout before and after sort,字符串按字典順序排序 指針數(shù)組,char *ptrN = “Pascal“,“Basic“,“Fortran“, “Java“,“Visual C“; char *temp=NULL; / for (i=0; iN-1; i+) for (j = i+1; jN; j+) if (strcmp(ptrj, ptri) 0) temp = ptri; ptri = ptrj; ptrj = temp; ,Memory layout before and after sort,main function with parameters,main(int argc, char *argv ) where:argc arguments number *argv pointer array points to the arguments.,command line: program name a1 b2 c3 d4,argv1,argv0,argv2,argv3,argv4,/*program name test.exe */ void main(int argc, char *argv ) int i; printf(“argc=%dn”,argc); /*display argc */ for(i=0; iargc; i+) printf(“%sn”,argvi);/*display command line*/ Command line: test APPLE-PC COMPUTER,output: argc = 3 test.exe APPLE-PC COMPUTER,Array pointer is a pointer, which points to an array. The form: Type (* pointer_name) constant expression Example: int ( * pb) 4 ; -Above define a pointer pb, which points to a one-dimension array. There are four integer elements in this array.,Array Pointers (數(shù)組指針),integer,pb,(*pb) 0,integer,(*pb) 1,integer,(*pb) 2,integer,(*pb) 3,void main( ) static int day_tab213 = 0,31,28,31,30,31,30,31,31,30,31,30,31, 0,31,29,31,30,31,30,31,31,30,31,30,31 ; int y, m, d; scanf (“%d %d %d“, ,計算某年某月某日是這一年的第幾天,A Pointer points to a Pointer 指向指針的指針,如果指針變量中保存的是另一個指針變量的地址,這樣的指針變量就稱為指向指針的指針 多級指針 即多級間接尋址(Multiple Indirection) 定義形式: 類型關(guān)鍵字 *變量名;,void main() int i = 5; int *ip = ,void main() int i; char*ptr = “Pascal“,“Basic“,“Fortran“, “Java“,“Visual C“; char *p; /聲明指向指針的指針p p = ptr; /用p指向指針數(shù)組首地址 for (i=0; i5; i+) printf(“%sn“, *p); p+; ,Summary: Pointers and Arrays,A common use of pointers is pointing to arrays. They are intimately related in C. Pointers to arrays generally result in code that uses less memory space and executes faster. An unsubscripted array name in a functions argument list is equivalent to passing a pointer to the starting location of the array. “不帶下標(biāo)的數(shù)組名等同于一個常量指針” If X is an array of any time, then Xi can always be equivalently expressed as *( X + i ).,There is a summary about pointer -Try to grasp it!,p is a pointer variable which points to int type data.,q is a pointer array in where there are four pointer elements. Each pointer points to an integer.,w is an array pointer which points to a one-dimension array. Array includes four integer elements.,g is a function. * means return value of function g is a pointer which points to an integer.,y is a pointer. () means pointer y points to a function and return value is integer.,Outline,Introduction Pointer and Function Pointer and Array Dynamic allocation string, character array & pointer Examples,Memory layout for program,一個由C編譯的程序占用的內(nèi)存分為以下幾個部分: 1、棧區(qū)(stack):由編譯器自動分配釋放,存放函數(shù)的參數(shù)值,局部變量的值等。其操作方式類似于數(shù)據(jù)結(jié)構(gòu)中的棧; 2、堆區(qū)(heap):一般由程序員分配釋放,若程序員不釋放,程序結(jié)束時可能由OS回收 。堆區(qū)與數(shù)據(jù)結(jié)構(gòu)中的堆是不同的,其分配方式類似于鏈表; 3、全局區(qū)/靜態(tài)區(qū)(static):用于存放全局變量和靜態(tài)變量。初始化的全局變量和靜態(tài)變量在一塊區(qū)域,未初始化的全局變量和未初始化的靜態(tài)變量在相鄰的另一塊區(qū)域。程序結(jié)束后由系統(tǒng)釋放; 4、文字常量區(qū):存放常量字符串,程序結(jié)束后由系統(tǒng)釋放; 5、程序代碼區(qū):存放函數(shù)體的二進制代碼。,Dynamic allocation,memory allocation,void * malloc ( unsigned size );,If memory allocation succeed, return the initial address of the memory block, or else, return NULL.,Example:,int * p; p = (int *) malloc( 10 * sizeof(int ) ); /dynamic array if (p = NULL) printf(“No memory available”);,/main.c int a = 0; /全局初始化區(qū) char *p1; /全局未初始化區(qū) int main(void) int b; /棧 char s = “abc”; /棧 char *p2; /棧 char *p3 = “123456”; /1234560在常量區(qū),p3在棧上。 static int c =0; /全局(靜態(tài))初始化區(qū) p1 = (char *)malloc(10); p2 = (char *)malloc(20); /*動態(tài)分配得來的10和20字節(jié)的 區(qū)域在堆區(qū)。*/ strcpy(p1, “123456”); /*1234560放在常量區(qū),編譯器可能會將它與p3所指向的“123456”優(yōu)化成一個地方。*/ return 0; ,Dynamic allocation,freeing memory,void free( void* ptr);,#include void main ( ) int *p; p = (int *) malloc( 10 * sizeof(int) ); printf(“n Result:”); try ( p, 10); free(p); void try ( int a , int m ) int k; for ( k=0; km; k+ ) a k = k*10; for ( k=0; km; k+ ) printf (“%d,”, ak); ,Outline,Introduction Pointer and Function Pointer and Array Dynamic allocation string, character array & pointer Examples,the relation among string, character array & its pointer,A pointer-based string is an array of characters ending in the null terminator (0), which indicates where a string terminates in memory. A string can also be accessed via a pointer, which points to the first character in the string. Therefore, you can declare a string variable using an array or a pointer,String, character array & character pointer,字符串 一串以0結(jié)尾的字符在C/C+中被看作字符串 可通過字符數(shù)組和字符指針完成對字符串的操作 字符數(shù)組 每個元素都是字符類型的數(shù)組 char string100; 字符指針 指向字符類型的指針 char* p;,字符指針與字符數(shù)組,定義方法 char str10; /array char *ptr;/pointer 賦值方法 char str10 = “china”; char *ptr; 或: char str10; ptr = “china”; strcpy(str, “china”);,字符指針與字符數(shù)組,定義數(shù)組時,編譯時分配內(nèi)存單元且有確定地址; 定義字符指針時,如未對它賦初值,則其所指數(shù)據(jù)是不定的,因而直接使用是危險的。 e.g輸入字符串時 char str10; scanf(“%s“, str); /*正確*/ char *a; char *a; scanf(“%s”, a); char str10; /*錯誤,指針指向不明*/ a = str; scanf(“%s“, a); /*正確*/,字符串拷貝-字符數(shù)組實現(xiàn),/*函數(shù)功能: 字符串拷貝 函數(shù)參數(shù): 字符型數(shù)組from,存儲源字符串 字符型數(shù)組to,存儲目的字符串 函數(shù)返回值:無*/ void MyStrcpy(char to , char from ) int i = 0; while (fromi != 0) toi = fromi; i+; toi = 0; ,字符串拷貝示意圖-字符數(shù)組,字符串拷貝-字符指針實現(xiàn),/*函數(shù)功能: 字符串拷貝 函數(shù)參數(shù): 字符型指針from,指向源字符串 字符型指針to,指向目的字符串 函數(shù)返回值:無 */ void MyStrcpy(char *to, const char *from) while (*from != 0) *to = *from; from+; to+; *to = 0; ,字符串拷貝示意圖字符指針,計算實際字符個數(shù)-字符數(shù)組實現(xiàn),/* 函數(shù)功能: 計算字符串的長度 函數(shù)參數(shù): 字符型數(shù)組str,存儲字符串 函數(shù)返回值:字符串的長度 */ unsigned int MyStrlen(char str) int i ; unsigned int len = 0; for (i=0; stri!=0; i+) len+; return (len); ,計算實際字符個數(shù)-字符指針實現(xiàn),/* 函數(shù)功能: 計算字符串的長度 函數(shù)參數(shù): 字符型指針變量pStr,指向字符串 函數(shù)返回值:字符串的長度 */ unsigned int MyStrlen(char *pStr) unsigned int len = 0; for (; *pStr!=0; pStr+) len+; return (len); ,Summary of data type,基本數(shù)據(jù)類型 int、long、char、short、float、double 數(shù)組、結(jié)構(gòu)體也是數(shù)據(jù)類型 是從其它類型派生的類型 每個元素或成員都有一個類型 指針是一種數(shù)據(jù)類型 是從其它類型派生的類型 “某某類型的指針” 任何類型都可以做指針或者數(shù)組的基礎(chǔ)類型 它們自己也可以做彼此或自己的基礎(chǔ)類型,Outline,Introduction Pointer

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論