#includestdio.h
創(chuàng)新互聯(lián)主營永順網(wǎng)站建設(shè)的網(wǎng)絡公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),永順h5微信小程序搭建,永順網(wǎng)站營銷推廣歡迎永順等地區(qū)企業(yè)咨詢
int main()
{
void copy(char *from,char *to);
char a[81]; //源串數(shù)組
char b[81]; //目標串數(shù)組,此數(shù)組要保證不小于源串,不然,數(shù)據(jù)會產(chǎn)生越界問題
printf("input a string:" );
gets(a); //輸入一個字符串數(shù)據(jù),如:hello,world
copy(a,b);
printf("%s\n",b);
return 0;
}
void copy(char *from,char *to)
{
for(;*from!='\0';from++,to++)
{
*to=*from;
}
*to='\0';
}
#include stdio.h
#include string.h
int copystring(char *str2, char *str1);
int main()
{
char str1[30] = "hello,string copied!\n";
char str2[30];
printf("str2[30]=%d\n", copystring(str1, str2)); // 你的copystring函數(shù)需要的參數(shù)是兩個字符型指針,而數(shù)組名本身就可以作指針來使用,str1[30]指的是字符數(shù)組str1中第31(從0開始,這里實際上越界了)個元素的地址
return 0;
}
int copystring(char *str2, char *str1)
{
printf("str2 is %s\n", str2);
strcpy(str1, str2); // strcpy函數(shù)第一個參數(shù)是復制后存放的數(shù)組,第二個才是要復制的對象
return *str2; // 我不太理解你這個函數(shù)想返回什么,你現(xiàn)在做的是將str2[0]的值以整型返回(h的ASCII碼對應104)
}
如果還有什么問題可以追問
現(xiàn)寫的 差不多 看好給分
字符串復制
char* string_copy(char* pdest, char* psource)
{
if(psource == NULL)
{
pdest = NULL;
return pdest;
}
while ((*pdest++=*psource++)!='\0');
return pdest;
}
字符串連接
char *string_strcat(char* s1, char* s2)
{
char *p = s1;
while(*p++);
--p;
while(*p++ = *s2++)
return s1;
}
字符串比較長度
int string_compare(char* s1, char* s2)
{
char *p1 = s1;
char *p2 = s2;
int i =0, j=0;
while(*p1++)
++i;
while(*p2++)
++j;
if (i j) return 1;
if (i == j) return 0;
if (i j) return -1;
}
本文名稱:字符串復制函數(shù)指針c語言 用指針實現(xiàn)字符串的復制
文章位置:http://m.kartarina.com/article32/dodsisc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站排名、用戶體驗、標簽優(yōu)化、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)