Search code examples
cargumentsparameter-passingmemory-addressstrcat

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;
}
  1. system told me different levels of indirection between “strcat”:“void (char *,char *)”and“char *(char *,const char *)”. this is the first why?
  2. after i change from void strcat(char s[], char t[])to void* strcat(char s[], char t[]) there is no error. so it means i can only use pointer void* to pass the address of str1 return type of char*, but i can't use the parameter s,t of strcat(char s[], char t[]) to modify the content of str1. if what i think is right,that will be the second why

Solution

  • 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.