c lang codesnippets

strcpy

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
char base_str[]="Erik was here.";

// Add the + 1 for the null terminator '\0'
char *cpy_str = (char *)malloc(strlen(base_str) + 1 * sizeof(char));
if(cpy_str == NULL){
fprintf(stderr, "Could not allocate memory for our strcpy.\n");
return -1;
}

strcpy(cpy_str,base_str);

fprintf(stdout,"Base str : -%s-\n", base_str);
fprintf(stdout,"Copied str : -%s-\n", cpy_str);

free(cpy_str);

return 0;
}



posted on 2012-03-30 16:39  grep  阅读(201)  评论(0编辑  收藏  举报