C语言学习感悟(2)

今天学习数组加上{0}避免打印结果出现乱码

#include 
#include 
#include 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(void) {
    char *p="la2b3d4z";
    char buf1[50]={0}; //这里要加入这个,要有指向,否则无法使用
    char buf2[50]={0};
    int ret=0;
    ret = getStr1Str2(p,buf1,buf2);
    if(ret!=0){
        printf("getStr1Str2 err:%d\n",ret);
        return ret;
    }
    
    printf("buf1=%s\n",buf1);
    printf("buf2=%s\n",buf2);
    
    printf("\n");
    system("pause");
    return 0;
}

int getStr1Str2(char *source,char *buf1, char *buf2){
    if(source==NULL || buf1==NULL || buf2==NULL){
        return -1;
    }
    
    int n=strlen(source);//字符串长度
    int i=0;
    for(i=0;i

使用函数strlen:用来计算指定的字符串s 的长度,不包括结束字符"\0"。
必须加入的包:#include


C语言学习感悟(2)_第1张图片
运行结果.png

你可能感兴趣的:(C语言学习感悟(2))