0

take a look at this :

char* s;
scanf("%s", s);

In general, s is a "memory box" that will contain an address to another one which, in this case, would be the first of a number of "memory boxes" containing chars before the "\0" marking the end of the string.
*s would let us access the "memory boxes", i.e the string.

Now back to the code and precisely scanf, I read it as follows : Take the user input, which is a string hence the "%s", and assign it to s. But how can this be? s is the reference, so shouldn't it be scanf("%s", *s) instead ?

  • 1
    Actually the code is not correct: https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-read-to-an-uninitializ – Jose Jun 20 '18 at 14:42
  • 1
    The [documentation for `scanf`](http://en.cppreference.com/w/c/io/fscanf) clearly states what types should be passed for which format specifier. Also `*s` would return a singular `char`, not a string. – UnholySheep Jun 20 '18 at 14:42
  • 1
    `char * s` does not create a "memory box". I think what you meant to say is that something like `char * s = malloc(80);` creates a "memory box" capable of holding 80 bytes, which must be freed later. – jdc Jun 20 '18 at 14:47
  • Yes you're right, that was not complete. Although, and i just tried, if I do malloc(1) then type a long text, printf("%s", s) shows that all the text was taken in, why is that? Shouldn't it stop at 1? –  Jun 20 '18 at 14:49
  • 2
    Best to use the phrase "pointer". It is the technical word and describes what it does. It is not the thing itself, it points to a thing. You should look for some examples of scanf(). Your example at time of writing is not correct. You need some memory into which the string will go. The string is a number of "char"s. You need a pointer to that memory. The pointer can just be to the first one. Then pass the pointer to scanf() and it will look for the string and put it i the memory the pointer points to. – William J Bagshaw Jun 20 '18 at 14:50
  • 1
    @archaic Reading many chars into a small buffer is a buffer overflow. A buffer overflow results in undefined behavior. Undefined behavior is precisely that. – jdc Jun 20 '18 at 14:54
  • Alright, thank you! –  Jun 20 '18 at 14:55
  • 1
    The C language is not "managed" and you can do what you like, even if it messes things up. If you do not give it enough space it does not know and just keeps going. Simplistically the memory is vast and uniform and C does not manage where things begin and end, you have to get it right. I think you need a working example. malloc() is not what you would find in a working example. – William J Bagshaw Jun 20 '18 at 14:56
  • 1
    To be honest I think you want to look at gets() as this does the same thing, You could find some example code for that and start to explore the language. – William J Bagshaw Jun 20 '18 at 15:03

1 Answers1

1

Let's take a simpler example:

char c;
scanf("%c", &c);

In this example, if you want to read a char, then you have to pass the address of that char (c), so that the method knows where to put the resulting char.

Let's expand to a string:

char *str;
scanf("%s", str);

In this case str is the address of the beginning of a string, therefore you could pass it to the scanf and expect the string to be read.

However, there's a catch here: str points to the beginning of the string, but this string doesn't actually exists, you'll have to create it. Like this:

char str[80];
scanf("%s", &str[0]);

As you can see here, str is now a string of 80 chars, and you can pass the location (address) of the beginning of this string to scanf. BUT, you have to pray that the string read is not bigger than 80 characters, otherwise the method will try to write outside the bounds of the string. You can correct this by telling scanf how many chars to read:

char str[80];
scanf("%79s", &str[0]);
AndreiM
  • 815
  • 9
  • 17
  • Thank you, initially I thought that in scanf the parameter is not the address of the "want to read" variable but the variable itself. –  Jun 20 '18 at 14:54
  • 1
    The last example was supposed to do exactly that, I updated it now – AndreiM Jun 20 '18 at 14:57
  • 1
    @archaic C is pass by value. You cannot pass "the variable itself" in any way to any function, all you ever get is a copy of the variable's value. – unwind Jun 21 '18 at 07:48