Your code is almost functional, but you're forgetting one thing and you have two errors.
What you're forgetting
You need to transfer to *content
each byte you're putting inside c
. Otherwise you'll simply overwrite c
hundreds of times for nothing.
Errors:
The first and most obvious error is that you shouldn't free()
the content, otherwise you'll lose it. The staff has writtenwrote code in main()
that will do the job of freeing content
when the server no longer needs it.
The second and more subtle error is that warning the compiler is giving you about the length
.
That happens because *length++
is not doing what you think. It's doing this:
- The dereference (
*
) happens first, giving you the contents of the memory location indicated bylength
. Let's suppose the number there is100
. - Then the expression is evaluated, the result of
100
is still100
. - The result is thrown away (you aren't doing anything with it).
length
is then incremented AFTER the evaluation. It's changing where the pointer is pointing to, not what it's pointing at.
It is effectively the same as doing this:
*length;
length = length + 1;
So the fix would be either:
- use
(*length)++
instead of*length++
to override the order of preference between the*
operator and the++
. - go the simple way and use
*length += 1;
or*length = *length + 1;