码迷,mamicode.com
首页 > 其他好文 > 详细

实现strlen()函数,strcmp()函数 const知识点

时间:2016-05-26 22:14:07      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:strlen()   strcmp()   关键字const   

1、strlen()函数的实现:

#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);
}

技术分享

2、strcmp()函数的实现:

#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));
}

技术分享

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离谁进,修饰谁,谁就不可更改!!!


注意:刚开始用Linux进行编程,(1). Linux下64位与32位的区别:

                  int都是4字节的。64位下,long 8字节, 指针 8字节

               (2).Linux下注释块:#if  0(注释)  1(不注释)

            

                        #endif 

               (3).gcc -c 只编译不连接  gcc   .c   -o  目标文件 编译和连接

               (4).objdump -d test(可执行文件) > x86  反汇编文件查看X86内容。

编译结果往往与平台,编译器关系很大!!!


  

实现strlen()函数,strcmp()函数 const知识点

标签:strlen()   strcmp()   关键字const   

原文地址:http://11596096.blog.51cto.com/11586096/1783613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!