There a several things to say about your code.
You can't copy strings using =
The return type is wrong. Must be char*
The scanf
format string is wrong. Use %s
for strings.
You don't need an extra array in the function. Just scanf
directly into s
Never do scanf("%s", ...
Always put a limit to it like scanf("%99s", ...
Something like:
char* str(char *s) {
printf("another string to replace:");
scanf("%99s",s);
return s;
}
void main() {
char s[100],s2[100];
scanf("%99s",s);
strcpy(s2, str(s));
printf("%s",s);
}
I am, however, not sure what you want the code to do. As it is now s
and s2
just end as identical strings.
If you just want a function that can read a new value into s
it can be done like:
void str(char *s) {
printf("another string to replace:");
scanf("%99s",s);
}
void main() {
char s[100];
scanf("%99s",s);
str(s);
printf("%s",s);
}