sizeof和strlen

1.sizeof

//cpu 32位下
#include <stdio.h>   
#include <string.h>      
char str[] = "Hello";    //sizeof(str) = 6   
struct size_b{       
    float f;      
    char p;       
}block; //sizeof(block) = 20  //4+4+3*4=20     
struct flag_s1{       
    char ch, *ptr;       
    union{           
        short a, b;           
    }u;       
    struct flag_s1 *next;
}s1;    //sizeof(s1) = 16 //4(ch)+4(ptr)+2(a)+2(b)+4(next)=16 
void func(char *fstr)   //sizeof(fstr) = 4 
{       
    printf("sizeof(fstr) = %d\n",sizeof(fstr));  
    printf("strlen(fstr) = %d\n",strlen(fstr));   
    //strlen(fstr) = 5 //strlen是求字符串的长度,不包括字符串的‘\0’,所以他的值比sizeof的值小一
}      
int main(void)   {       
    printf("sizeof(str) = %d\n",sizeof(str));     
    printf("sizeof(block) = %d\n",sizeof(block));       
    printf("sizeof(s1) = %d\n",sizeof(s1));      
    func(str);
    int *p=malloc(100); //sizeof(p) = 4;

    return 0;   
}

2.strlen

注意:遇到’\0’停止

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值