'indirect addressing problem about functions
strcat(s,t) copies the string t to the end of s,the following is initial code.
void strcat(char s[], char t[])
{
int i, j;
i = 0;
j = 0;
while (s[++i] != '\0');
while ((s[i++] = t[j++]) != '\0');
}
#define MaxSize 100
int main()
{
char str1[MaxSize] = { 'a','b','c','d' };
char str2[MaxSize] = { 's','s' };
strcat(str1, str2);
printf("%s", str1);
return 0;
}
- system told me different levels of indirection between “strcat”:
“void (char *,char *)”and“char *(char *,const char *)”. this is the first why? - after i change from
void strcat(char s[], char t[])tovoid* strcat(char s[], char t[])there is no error. so it means i can only use pointervoid*topass the address of str1return type of char*, but i can't use the parameter s,t ofstrcat(char s[], char t[])to modify the content of str1. if what i think is right,that will be the second why
Solution 1:[1]
thank you guys very much, i should change the function name. e.g. void strcat(char s[], char t[]) should be modified to void mystrcat(char s[], char t[]).
i want to use my own version of strcat but forgot it violated the C language specification. because i wrote code # include< string.h>.
after i realize this problem, void strcat(char s[], char t[]) and void strcat(char s[], char t[]) are both OK.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
