I’m getting the compile error on the line “new_str = combine_string(newstr, “test”);” Error: passing arg 1 of `combine_string’ from incompatible pointer type
char * combine_string(char *, char *);
....
char *new_str;
new_str = newstr(item, strlen(item));
new_str = combine_string(newstr, "test");
...
char * combine_string(char *name, char *name2)
{
char *retval;
retval = erealloc(retval, (strlen(name) + strlen(name2) + 1));
if (retval != NULL)
sprintf(retval, "%s%s", name, name2);
else
free(name); //Wouldn't use it any longer
return retval;
}
...
char *newstr(char *s, int l) {
char *rv = emalloc(l + 1);
rv[l] = '\0';
strncpy(rv, s, l);
return rv;
}
The way you have it declared,
newstris a function, whilenew_stris a char*.You probably meant to pass in
combine_string(new_str, "test");instead of how you have it.I might suggest giving your variables and functions more descriptive names in the future to avoid these kinds of things!
EDIT: If you’re wanting to use the return value of a call to
newstr()as arg 1 ofcombine_string()then you will have to pass the proper parameters in tonewstr()like so