Is it right to write like this ?:
void main() { char *path; scanf("%s", path); }
In the dosovsky compiler (qc2) it works without problems, errors and errors. Yes, and the teacher said that the correct way to write than to create a static array.
There would be no questions if it were not for one bad luck. In the gcc compiler at this point the program is interrupted (segmentation error).
Here it is worth clarifying: the code above will also work in gcc, however, this code is no longer operational:
void main() { char *path; int i; i = 0; scanf("%s", path); }
Why is everything OK in the old compiler, but not in the modern one? Or is it wrong to write like that?
UPD 1:
@KoVadim , thanks, clarified.
@Janycz , in this case there seems to be no particular difference, dynamic or static, because the length of the string is unknown in advance.
And as a matter of fact, how in that case to read a line, not knowing its length? After all, to create a large array is unwise.
scanf
does not cause an overflow (there is an opportunity to limit the size of the read line). Or do the reading itself character-by-character and allocate memory accordingly (though, all the same, the memory will need 2x the length of the read string) - gecubeman 3 getline
. For Windows, write your analogue (a few dozen lines, good training). - avp