Mail Archives: djgpp/1998/08/16/04:58:58
On 15 Aug 1998, Cephaler wrote:
> This yielded 'raboof' to my delight... Now, have I found a good use for
> pointers? Or is this bad bad code?
It's one of the uses. I don't see anything particularly bad here,
except that you never check the values returned by `malloc' (they might
be NULL pointers if `malloc' fails to allocate), and you rely on "foobar"
to be shorter than the size of the buffer allocated by `malloc'.
> 2) Having not initialized string2
But you *did* initialize string2, by pointing it to the same address
where string1 points.
> a) do I have to free string2 and b) does
> that have any effect on string1? (oops didn't free string1)
You need to free any of them (but not both!).
> 3) concerning strcpy...is there any special reason why I shouldn't just use
> string1 = "foobar" ?
THis is something else. It doesn't copy "foobar" at all, it changes the
value stored in string1. After this, string1 points to the address where
the constant string "foobar" is stored.
- Raw text -