TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [TCLUG:10678] Simple C Question
On Wed, Dec 01, 1999 at 10:36:54AM -0600, Brian Ackermann wrote:
> Ok, simple question for all you C programmers out there...
>
> Here's the line of code in question...
>
> printf("%s\n", (char *)(FO_Offset+=16));
>
> Assuming FO_Offset=0 at start and this code runs in a loop we should
> see:
>
> 16
> 32
> 48
> 64
> 80
> 96
>
> and so on...
>
> I'm assuming that (FO_Offset+=16) evaluates to an integer value, say,
> 16, in
> the first instance,
> and then casting it to a (char *) will let the printf() do what it
> does.
>
> The program compiles without errors or warnings, but the text is just
> garbage...
>
> What am I missing?
*puzzled look*
The approach you're taking to the problem shows a distinct lack
of understanding of what a C string is. To explain your misconception
would require talking to you a lot to find out exactly what it was, and
then a lengthy explanation. I suggest you find a programmer to talk to
in person.
The answer is this... You code _should_ read:
print("%d\n", FO_Offset += 16);
It's bad style to increment FO_Offset in the same statement like
that. It makes your code much harder to read. I would suggest:
FO_Offset += 16;
print("%d\n", FO_Offset);
instead.
Like I said, please go find someone to explain C strings and how
they work to you.
Have fun (if at all possible),
--
Its name is Public Opinion. It is held in reverence. It settles everything.
Some think it is the voice of God. Loyalty to petrified opinion never yet
broke a chain or freed a human soul. ---Mark Twain
-- Eric Hopper (hopper@omnifarious.mn.org
http://ehopper-host105.dsl.visi.com/~hopper) --
PGP signature