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 04 12:04

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 Fri, 2 Jun 2006 11:31:43 -0400
p@dirac.org (Peter Jay Salzman) wrote:

> What I'm getting at is this.  Because all the chars have the same
> width, it doesn't matter WHAT kind of pointer you pass in to a
> function: char, signed char, or unsigned char.  Pointer arithmetic
> just works, and it works because they all have the same width.
> On the other hand, the data is what gets mangled if you don't use the
> correct type:
>    char c = 255;
>    printf("%d", c);
> prints, as expected, -1.  Not 255.
> So it seems to me that if the compiler complains about anything, it
> should complain about passing a different type of char, not a
> different type of char *.

unsigned char c = 255;
signed int i;
signed int j;

signed int someFunction(char * nativeChar)

i = someFunction(*c);
j = c;

(I'm extremely rusty on my c pointer syntax and don't have a book handy,
I'm trying to pass a pointer to "c" and return "c" converted to signed 
integer. I hope I didn't screw that up too badly.) Assume for the
moment the compilers don't complain about the different pointer types.

Compiler #1 treats native char type as unsigned.
Compiler #2 treats native char type as signed.

When you switch from compiler #1 to compiler #2:
What happens to the value of i?
What happens to the value of j?
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.