linux中strlen的用法,实现strlen()函数,strcmp()函数 const知识点

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

发布时间:2020-08-03 05:49:13

来源:51CTO

阅读:576

作者:汇天下豪杰

1、strlen()函数的实现:#include

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

}

6f5e2cbc8aac9c3ebdda907948cf51d4.png

2、strcmp()函数的实现:#include

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

}

2c404e36021a21d4720b7f24b1863603.png

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内容。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值