Discussion:
"PORTING C" > when to cast parameters?
(too old to reply)
Robby
2010-01-12 17:35:01 UTC
Permalink
Hello,

Quick question!

Since I have a quite a few variables which must stay as 8 bits, I find
myself having to pass many of these to functions as parameters.

So if I had:

int x;

I am now calling it:

unsigned char x;

To me, "x" is still an 8 bit value which actually simulates my old int's.

However, many of my functions from the 8 bit processor are like this:

void f1(int x);

Which means that "x" in the new 32 bit processor is considered as 32 bits !!!!

I don't want to change any of my function prototypes to:

void f1(unsigned char x);

which means that I would leave them as they are with int's.

So, my question is, in general, do C/C++ compilers do automatic casting when
a parameter in the calling function is one type, but the function definition
is another.

For example, if I do this:

======================

void f1(int x)
{x++;}

int main(void)
{unsigned char x=10;
f1(x);
return 0;}
======================

should I trust the compiler to do the necssary cast from char to int?

*OR*

should I always do it myself, like this:

======================

void f1(int x)
{x++;}

int main(void)
{unsigned char x=10;
f1((int) x);
return 0;}
======================

Thnakyou all for your help!
--
Best regards
Roberto
Igor Tandetnik
2010-01-12 18:46:21 UTC
Permalink
Post by Robby
unsigned char x;
void f1(int x);
So, my question is, in general, do C/C++ compilers do automatic
casting when a parameter in the calling function is one type, but the
function definition is another.
Between some types, yes. Passing unsigned char where int is required shouldn't be a problem, this conversion is lossless.
Post by Robby
void f1(int x)
{x++;}
This is rather pointless. You are taking a parameter by value, so any changes to it inside the body of the functions aren't visible to the caller. Your function behaves the same as

void f1(int x) {}
Post by Robby
int main(void)
{unsigned char x=10;
f1(x);
return 0;}
======================
should I trust the compiler to do the necssary cast from char to int?
Yes, this would work (except for the fact that f1 doesn't do what you seem to think it does).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Robby
2010-01-12 19:43:02 UTC
Permalink
Hello Igor,
Between some types, yes. Passing unsigned char where int is required shouldn't >be a problem, this conversion is lossless.
Okay, then! I will remove the int type casts then!
Yes, this would work (except for the fact that f1 doesn't do what you seem to think >it does).
void f1(int x)
{x++;}
Oh I am very aware that f1() does nothing, this was just done this way for
sample purpose. If f1() would be done this way it would be for something like
this:

void f1(int x)
{
// ... other code!!!
PORTWrite(IOPORT_D, x);
}

which would write the value of x to port D of the processor! And where the
value of x would not bee needed to be visible to the caller.

Thankyou for your reply!
--
Best regards
Roberto
Barry Schwarz
2010-01-13 05:11:52 UTC
Permalink
On Tue, 12 Jan 2010 11:43:02 -0800, Robby
Post by Robby
Hello Igor,
Between some types, yes. Passing unsigned char where int is required shouldn't >be a problem, this conversion is lossless.
Okay, then! I will remove the int type casts then!
There are no type casts in C. The operand of a cast operator is
always a value. (Contrast with the sizeof operator where the operand
is always a type.)

Going back to your original message, there are no automatic casts in
C. A cast occurs only when a cast operator is coded in the
expression. There are automatic conversions between compatible types
which is what you described.
--
Remove del for email
Igor Tandetnik
2010-01-13 05:33:51 UTC
Permalink
There are no type casts in C. The operand of a cast operator is
always a value.
That's splitting hairs. "Type cast" is a common colloquial term meaing roughly "an expression involving a cast operator". In fact, C99 standard uses the term once - see H.2.4p1 (non-normative).
(Contrast with the sizeof operator where the operand
is always a type.)
Not true. You can write sizeof(expression). Though I don't see how this relates to the issue of type casts.
Going back to your original message, there are no automatic casts in
C.
But there are implicit type conversions. Sounds like a distinction without a difference to me.
A cast occurs only when a cast operator is coded in the
expression. There are automatic conversions between compatible types
which is what you described.
Implicit conversions, if you insist on being pedantic.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Barry Schwarz
2010-01-14 02:46:31 UTC
Permalink
On Wed, 13 Jan 2010 00:33:51 -0500, "Igor Tandetnik"
Post by Igor Tandetnik
There are no type casts in C. The operand of a cast operator is
always a value.
That's splitting hairs. "Type cast" is a common colloquial term meaing roughly "an expression involving a cast operator".
In fact, C99 standard uses the term once - see H.2.4p1 (non-normative).
(Contrast with the sizeof operator where the operand
is always a type.)
Not true. You can write sizeof(expression). Though I don't see how this relates to the issue of type casts.
You may write sizeof(expression) but the operand is a type. 6.5.3.4-2
says "The size is determined from the type of the operand."
Post by Igor Tandetnik
Going back to your original message, there are no automatic casts in
C.
But there are implicit type conversions. Sounds like a distinction without a difference to me.
A cast occurs only when a cast operator is coded in the
expression. There are automatic conversions between compatible types
which is what you described.
Implicit conversions, if you insist on being pedantic.
See the first sentence of 6.3-1.
--
Remove del for email
Igor Tandetnik
2010-01-14 03:03:15 UTC
Permalink
Post by Barry Schwarz
On Wed, 13 Jan 2010 00:33:51 -0500, "Igor Tandetnik"
Post by Igor Tandetnik
Post by Barry Schwarz
(Contrast with the sizeof operator where the operand
is always a type.)
Not true. You can write sizeof(expression). Though I don't see how
this relates to the issue of type casts.
You may write sizeof(expression) but the operand is a type. 6.5.3.4-2
says "The size is determined from the type of the operand."
If operand is a type, then "type of the operand" becomes "type of the type", which doesn't make much sense. No, the operand is an expression, and the result of sizeof is the size of the type of that expression.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Ulrich Eckhardt
2010-01-13 07:52:16 UTC
Permalink
Post by Robby
Since I have a quite a few variables which must stay as 8 bits, I find
myself having to pass many of these to functions as parameters.
int x;
unsigned char x;
To me, "x" is still an 8 bit value which actually simulates my old int's.
void f1(int x);
Which means that "x" in the new 32 bit processor is considered as 32 bits !!!!
If you want 8 bits, say that. C99 introduced types via <stdint.h> like
int8_t and uint8_t. Use those in the code. If your compiler doesn't have
<stdint.h>, create according typedefs as workaround in a central header of
your project.
Post by Robby
So, my question is, in general, do C/C++ compilers do automatic casting
when a parameter in the calling function is one type, but the function
definition is another.
Yes, the types will be implicitly converted. If the conversion is not
possible, you will get an error. You should turn on warnings in any case
though, some implicit conversions are unhealthy.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Continue reading on narkive:
Loading...