Kon-Tiki wrote:
Ok, that works now, but the output gives weird ascii-codes. This's with converting and reconverting, IIRC, but dunno how to go against it. Oh well, I'll follow Tim's advice and start reading from the moment I know that I won't waste my time on the book I have. Maybe I'll be able to figure it out then.
The specific problem is in the code Joel wrote for calling the functions:
Code: Select all
input(seat, pos, object);
// same goes for the condition function
condition(seat, pos, object);
This should be:
Code: Select all
input(&seat, &pos, &object);
// same goes for the condition function
condition(&seat, &pos, &object);
The reason for this is that in main(), seat[], pos[], and object[] are defined as 10 element arrays; since an array is
not a pointer, per se, but an identifier for a block of memory, you need to use & to get the addresses of the arrays. Otherwise, the arguments passed to input() and condition() will be the ASCII values of the first four elements of each array (assuming 32-bit addressing), not the pointers to the arrays as expected.
This is a common mistake, and no real fault of Joel's; this is one of the hardest parts of C to understand, especially if you don't know the underlying assembly language, and even then it is an easy mistake to make (since it is more typical for c-strings to be handled through pointers anway). HTH, but if you don't get it, don't stress over it too much just yet.
BTW, this also will help understand scanf(): it takes a format string, followed by an arbitrary number of pointers (it can do this because of the particular way that C pushes arguments onto the stack; for now, don't worry about how it works too much yet). It reads the format string, which tells it the number and type of arguments; it then uses the pointers to put the scanned values into the variables which they point to. As you may have noticed, this is a rather error-prone approach, but used carefully can be quite effective.