strcpy\strlen\sizeof 认识

c++程序网站 
http://www.ok2002.com/cc/cc/s.asp?r=a1999113275160

    char *strcpy(char *strDest, c o n st char *strSrc);//第二个参数设置为常量 是不想在函数中间 让其发生改变
    {
        assert((strDest!=NULL) && (strSrc !=NULL)); // 这句是断言 判断不该出现的错误
        //如果出现作物 立即在这里停止
        char *address = strDest;//创建一个地址指针 用于返回      
        while( (*strDest++ = * strSrc++) != ‘\0’ )  // 2分
            //这句比较复杂  
            //(*strDest++ = * strSrc++) 这个是逐位赋值
            //而每次赋值之后 用*strDest!='/0'进行比较作为循环条件结束 也就是 strSrc已经复制到结束了
            return address ;//最后返回       
    }
    
    
    char p1[] = "i love you"; //8个字母+2个空格  < ***申请空间要写p1[11]因为字符串多了一个 \0*** >
    char p2[] = "0123456789012";
    
    //因为p2 > p1这里报错显示越界 vc6.0不报错
    //strcpy(p1, p2);
    
    strncpy(p1, p2, strlen(p1));
    
    //加了printf则NSLog就不显示
    //printf("%s",p1);
    
    //strlen 返回实际字符数
    //sizeof 它返回的是参数的全部内存,包括使用的和未使用的 例1:p1[20] sizeof(p1) = 20   例2:sizeof(p1[50]) = 1 因为char只占一个字符
    //%lu 无符号长整型long unsigned
    NSLog(@"p1 = %s", p1);
    NSLog(@"strlen p1 = %lu", strlen(p1));
    NSLog(@"sizeof p1 = %lu", sizeof(p1));
    NSLog(@"strlen p2 = %lu", strlen(p2));
    NSLog(@"sizeof p2 = %lu", sizeof(p2));
    
    /*
     2012-08-16 09:59:10.617 appDemo[940:f803] p1 = 0123456789
     2012-08-16 09:59:10.618 appDemo[940:f803] strlen p1 = 10
     2012-08-16 09:59:10.619 appDemo[940:f803] sizeof p1 = 11 //比strlen多算了一个结尾的空字符 \0
     2012-08-16 09:59:10.619 appDemo[940:f803] strlen p2 = 13
     2012-08-16 10:23:52.515 appDemo[940:f803] sizeof p2 = 14
     */

posted @ 2012-09-07 13:29  六界剑仙  阅读(146)  评论(0)    收藏  举报