第四章 语句 [c和指针]

来源:互联网 发布:gaga软件下载 编辑:程序博客网 时间:2024/06/03 12:31

C中的常用语句if、for、while、do、switch、break、continue、goto;没什么好说的;

写了写基础性很强的小例程,课后 4-4

/* 4-4 realization of strcpy function,that is to say copy n char to destination,fill null if necessary*/void copy_n(char dst[], char src[],n){    if(strlen(src)< n)    {        for(int i = strlen(src);i < n; i++)            src[i] = ' ';    }    for(int i = 0,j = 0; i< n; i++,j++)    {        dst[j] = src [i];    }}


参考程序

void copy_n(char dst[], char src[],n){    int dst_idx, src_idx;    src_idx = 0;    for(dst_idx = 0; dst_idx < n; dst_idx++)    {        dst[dst_idx] = src[src_idx];        if(src[src_idx] != 0)            src_idx++;    }}


 

原创粉丝点击