Rich Touper
7/1/2011 10:14:00 PM
On Fri, 01 Jul 2011 17:52:35 -0500, Shao Miller wrote:
> On 7/1/2011 4:21 PM, Rich Touper wrote:
>> C allows type casting in which a variable is converted from one type to
>> another.
As everyone pointed out, "converted" should be "promoted", and "during
evaluation" should have been tacked on the end. That would have helped
on the semantics of the question, but is still vague.
> Hmm... Not that I know of. As far as I know, there isn't any "type
> casting" in C, but a value of some type can be converted to a value of
> some other type.
That seems to be the concensus of opinion. I certainly never heard of
it, but thought it may be an obscure, rarely used feature.
>> Does C (whatever standard) allow the type of a variable to change,
>> within a statement, avoiding the conversion? And if so, provide an
>> example of how its done.
>>
>> Example:
>> int a=23;
>> int b=34;
>> char c='a';
>> int * p_int=(int *)&c;
>>
>>
> A few concerns with that last line including "alignment issues, storage
> size issues, etc."
Wasn't attempting to start a huge thread on pointers, just using it to
illustrate a point.
>> // type casting
>> // c is converted into type integer, then b is added. (int) c + b;
>>
>>
> I don't think so. The value of 'c' is converted to an 'int' value, that
> is added to the value of 'b', then the result is unused. 'c' remains a
> 'char' throughout.
Exactly. The pomotion happens according to the standard, but only the
value of c is used, and because c is type char, 1 byte.
>> //retypeing
>> (type int) c + b;
>
> What is 'type'? It's not a C keyword. Is it some macro you haven't
> shared?
I don't have a way to define it. But the point is IF c could be "retyped"
to an int, then sizeof(int) bytes would be used during evaluation. As
other posters pointed out, there would be alignment issues, unknown bytes
at &c+1 to &c+sizeof(int), etc...
I'll take this as a resounding NO. No such thing as "retyping".
Thanks
>> // similar action
>> *p_int + b
>>
>>
> Assuming the original initialization of 'p_int' succeeded and still
> points to the first byte of the object identified as 'c', the expression
> '*p_int' attempts to re-interpret the bytes at the address of 'c' as an
> object representation for an 'int' value.
>
>> Ignoring alignment issues, storage size issues, etc., c is retyped to
>> an integer, no conversion is done, and c is used, within the statement,
>> as an integer, not a char.
>
> You can do similar things with unions for re-interpreting an object
> representation of a value of one type as an object representation of a
> value of a different type.
>
> I don't understand why one would wish to ignore alignment issues,
> storage size issues, etc.
>
> To answer the only question in this post, you might be interested in C's
> definition of "effective type." For example, in:
>
> void * vp = malloc(sizeof (int[10]);
>
> if 'malloc' succeeds, the object pointed to by 'vp' has no declared type
> and no effective type. An effective type can later be established by
> using an lvalue to read or modify the object.
>
> int (* ten_ints)[10] = vp;
>
> if (ten_ints) {
> (*ten_ints)[0] = 1;
> /* ... */
> (*ten_ints)[9] = 10;
> }
>
> Now the effective type for the allocate storage has been established as
> the type 'int[10]'.