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

Jeffrey J. Nonken jjn_lugod at nonken.net
Sun Jun 4 08:02:26 PDT 2006


On Fri, 2 Jun 2006 11:31:43 -0400
p at 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)
{
	return(&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?


More information about the vox-tech mailing list