字符串問題--C裏面的strcpy函數

源代碼:

  char s1[]="12345",*s2="123467";
  printf("s1 is:%s\n",s1) ;
  printf("s2 is:%s\n",s2) ;
  printf("s1's strlen:%d\n" ,strlen(s1));
  printf("s1's sizeof:%d\n" ,sizeof(s1));
  printf("s2's strlen:%d\n" ,strlen(s2));
  printf("s2's sizeof:%d\n" ,sizeof(s2));
  printf("strcpy s1 is:%s\n",strcpy(s1,s2)) ;
  printf("strlen %d\n" ,strlen(strcpy(s1,s2)));
  printf("sizeof %d\n" ,sizeof(strcpy(s1,s2))); 
strlen:字符長度

sizeof:1.類型長度2.字符長度+\0的長度也就是strlen+1

[引用]sizeof returns the size counted in bytes, where the C definition of ``byte'' is ``the size of a char.'' In other words, sizeof(char) is always 1. (It turns out that it's not necessarily the case, though, that a byte or a char is 8 bits.)點擊打開鏈接

strcpy:將目的字符串複製給源字符串,並且將字符串覆蓋,但是不改變原數組長度

1.遇到\0停止複製 2.如果源空間不夠則溢出,並且停止

char s1[]="1234",*s2="123456789";


char s1[]="12345",*s2="1234";


char s1[]="123456\0789",*s2="1234";


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章