溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

實現(xiàn)strlen()函數(shù),strcmp()函數(shù) const知識點

發(fā)布時間:2020-08-03 05:49:13 來源:網(wǎng)絡(luò) 閱讀:683 作者:匯天下豪杰 欄目:編程語言

1、strlen()函數(shù)的實現(xiàn):

#include<stdio.h>

int strLen(char *str);

int strLen(char *str){
    int i = 0;
    
    while(*str){
        str++;
        i++;
    }
    
    return i;
}

void main(void){
    char *str = "abcdefg";
    int length;
    
    length = strLen(str);
    printf("%d\n", length);
}

實現(xiàn)strlen()函數(shù),strcmp()函數(shù) const知識點

2、strcmp()函數(shù)的實現(xiàn):

#include<stdio.h>

int strCmp(char *str1, char *str2);

int strCmp(char *str1, char *str2){
    while(*str1 == *str2 && *str1 && *str2){
        str1++;
        str2++;
    }
    
    return *str1 - *str2;
}

void main(void){
    char *str1 = "hello";
    char *str2 = "hell";
    
    printf("%d\n", strCmp(str1, str2));
}

實現(xiàn)strlen()函數(shù),strcmp()函數(shù) const知識點

3、const的用法:

const只讀。

(1)const int a = 100 <=> int const a = 100;  a空間是只讀空間,a空間的值不能更改。

(2)const int *a; <=> int const *a;  *a的值不能改變,a指針變量的值可以更改。

  int* const a;  *a的值可以更改,a指針變量只讀,不能改其值

  const int* const a;   *a, a 均只讀空間,其值不可更改!

const離誰進,修飾誰,誰就不可更改?。。?/span>


注意:剛開始用Linux進行編程:

  (1). Linux下64位與32位的區(qū)別:

  int都是4字節(jié)的。64位下,long 8字節(jié), 指針 8字節(jié)

  (2).Linux下注釋塊:#if  0(注釋)  1(不注釋)

           ...........  

           #endif 

  (3).gcc -c 只編譯不連接  gcc   .c   -o  目標(biāo)文件 編譯和連接

  (4).objdump -d test(可執(zhí)行文件) > x86  反匯編文件查看X86內(nèi)容。

編譯結(jié)果往往與平臺,編譯器關(guān)系很大?。。?/span>


  

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI