I have to scan a string with spaces so I can do somethings with it in this case moving the string n chars to the right or to the left. For example:
If I give n = 1, the string
house
becomes:
ehous
But the input can also be a string with spaces like, n being the same:
yellow house
becomes:
eyellow hous
So to scan the string im doing this:
char text[166];
scanf("%d %[^\n]%*c", &n, &text);
And everything works great, but now the I submitted the program I get this error:
src.c: In function 'main':
src.c:30:26: error: format '%[^
' expects argument of type 'char *', but argument 3 has type 'char (*)[166]' [-Werror=format=]
30 | scanf("%d %[^\n]%*c", &nVezes, &texto);
| ~~~^~ ~~~~~~
| | |
| char * char (*)[166]
What can I solve this? Also I can't use these libraries string.h, strings.h, and stdlib.h.
Every bit of help is appreciated.
scanf()
does not truly scan a string but stdin
.
Instead OP want to read a line of user input into an int
and string .
Save headaches - ditch scanf()
with its many weaknesses.
// v---------------------- missing width limit
// v------- do not use here
scanf("%d %[^\n]%*c", &n, &text);
// ^ ^ ----------------------- possible to read more than 1 line
// return value not checked
char text[166];
char line[sizeof text + 20];
if (fgets(line, sizeof line, stdin)) {
if (sscanf(line, "%d %165[^\n]", &n, text) == 2) {
Success(); // OP's code here
}
}
You don't need to pass the char array as a pointer(*). when you pass an array in C as an argument to a function, the name of the array acts as a pointer to the start of the array.
So, you should pass the array as follows:
scanf("%d %[^\n]%*c", &n, text);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.