l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2006 Jun 05 08:59

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] C - passing chars and pointer to chars
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] C - passing chars and pointer to chars

On Sun, 4 Jun 2006 13:29:08 -0400
p@dirac.org (Peter Jay Salzman) wrote:

> the "value" of i and j is the same "value" as c.  the only provisio
> is how that value is interpreted.  since i and j are both signed
> ints, when you print them with printf() or cout, the pattern will be
> interpreted as an unsigned int, that is, 255.  not -1.
> to summarize, someFunction() doesn't change the value of c.

If the compiler treats native char as unsigned:

i will be 0x000000ff or 255.
j will be 0x000000ff or 255.

If the compiler treats native char as signed:

i will be 0xffffffff or -1.
j will be 0x000000ff or 255.

That's because when it converts -1 from a signed char (8-bit) to a
signed int (16 bit or larger, I'm assuming native 32 bit here, but it
doesn't matter) it does a sign extend. When you pass an unsigned
pointer and cast it as a signed pointer you treat the value in the
variable differently.

Shrug it off if you will, but -1 and 255 are VERY different values.
Treating them as the same can cause some real headaches. Depending on
one native implementation and then porting to another platform that
might have a different implementation can cause some pretty bad
headaches too, because things that worked just fine suddenly break for
no obvious reason. (For example, expecting a native 16-bit integer to
wrap above 0xffff and then porting to an implementation that uses native
32-bit integer. And if that sounds like the voice of experience
talking, well, it is. :)

THAT is why char * and unsigned char * and signed char * are three
entirely different animals and should be treated as such.

And when you DO figure out the reason you spend the next month
sifting through the code trying to find all the instances of native
whatever and trying to determine whether each one needs to be changed
to explicit signed, unsigned, short, long, and so on. That's why I like
to be explicit up front.

And I would not use printf()'s behaviour as the only yardstick for this
vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.