To save a string of unknown size without spending extra memory, you need to read the street name in a buffer, a very large variable that can support any street name, for example, size 1000. Then use the string library. h and its strlen() function, which returns how many characters a string has and use strlen(buffer).
Now you know how many characters have the street name, if you do
char buffer[1000];
scanf("%s", buffer);
int a = strlen(buffer);
You can use the malloc() function of the stdlib. h library to store how much memory you need, for example:
char *nome_rua = (char *)malloc(sizeof(char)*a);
Then you can copy the contents of the buffer to your variable, and use the strcpy() function of the string library. h
Source: https://www.tutorialspoint.com/c_standard_library/string_h.htm
You need to malloc (a+1) positions, because the string must have O, which delimits its end.
And also, you can use the function
char *strncpy(char *dest, const char *src, size_t n)
Because then you would only copy from the buffer the amount of characters read.
But using buffer[1000] I am not limiting the string size to 1000?
– pp fernandes
Well, you can put the size of this very large buffer, for example, 10,000. Or go reading char by char, storing in an auxiliary variable (similarly) and controlling how many char’s were read to allocate only the necessary
– Gabriel Pellegrino
Allowing unlimited entry is not a good idea. If someone wants to lock your application, you will easily get.
– mau humor
Another question is how I pass after the buffer to "Ecoponto.rua" tried with strcpy and did not give and I think it is because the street is as pointing in a structure.
– pp fernandes
It is that if it is probably a school work, it can be chosen a random (but, finite) value. And it can be a test you think about in this case.
– Gabriel Pellegrino
Thanks.I’ve been able to give it away but when I give a space between the words only copies the first word.
– pp fernandes
It was scanf("%s",buffer);' but this way it no longer copies to the "street".
– pp fernandes
Missing % in scanf("%[ n]s",buffer).
– pp fernandes