编译出现错误,查不出问题啊
小弟写了个小程序,就是查不出哪出错了,各位师傅帮帮忙啊,谢谢哈
用指针方式,将函数stract(s, t)将t指向的字符串复制到s指向的字符串的尾部。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int strlen(char *s);
void strcat(char *s, char *t);
void strcpy(char *s, char *t);
int main()
{
int i, j;
i = j =0;
char *words = "hello, world";
char *plus = ", I am comming.";
i = strlen( words );
j = strlen( plus );
printf("i = %d\nj = %d\n\n", i, j);
strcat(words, plus);
i = strlen( words );
printf("i = %d\n", i);
getch();
return 0;
}
int strlen(char *s)
{
char *p = s;
while ( *p != '\0' )
p++;
return p - s;
}
void strcpy(char *s, char *t)
{
while(*(s++) = *(t++))
;
}
void strcat(char *s, char *t)
{
while(*s)
++s;
strcpy(s, t);
}
[解决办法]
#include <stdio.h>#include <conio.h>#include <stdlib.h>int strlen(char *s);void strcat(char *s, char *t);void strcpy(char *s, char *t);int main(){ int i, j; //i = j =0; char words[100] = "hello, world"; //用char * 初始化words为"hello, world",不能通过words来修改"hello, world" char *plus = ", I am comming."; i = strlen( words ); j = strlen( plus ); printf("i = %d\nj = %d\n\n", i, j); strcat(words, plus); i = strlen( words ); printf("i = %d\n", i); printf("%s\n", words); //getchar(); return 0;} int strlen(char *s){ char *p = s; while ( *p != '\0' ) p++; return p - s;}void strcpy(char s[100], char *t){ int i = strlen(s); while(s[i++] = *(t++)) ;}void strcat(char s[], char *t){ //int i = 0; //while(s[i++]) //++s; strcpy(s, t);}
[解决办法]
char words[54] = "hello, world";、
改成这样,数组太小啦,words后面还要放", I am comming.",长度不够
[解决办法]
int strlen(char *s);void strcat(char *s, char *t);void strcpy(char *s, char *t);int main(){ int i, j; i = j =0; char words[50] = "hello, world"; char *plus = ", I am comming."; i = strlen( words ); j = strlen( plus ); printf("i = %d\nj = %d\n\n", i, j); strcat(words, plus); i = strlen( words ); printf("i = %d\n", i); printf("%s",words);// getch(); return 0;} int strlen(char *s){ char *p = s; while ( *p != '\0' ) p++; return p - s;}void strcpy(char *s, char *t){ while(*(s++) = *(t++)) ;}void strcat(char *s, char *t){ while(*s) ++s; strcpy(s, t);}