Puzzle on type casting an integer 
Author Message
 Puzzle on type casting an integer

HI, I have the following problem:

int *p1;
int *p2;
int offset=15;

p1 = p2 + offset;

Quote:
>> of course , ANSI C complains that type mismatch or incompatible

between the right and left side. what I want to do here is that I want
to assign an absolute memory address to p1. This address is the sum
of another address and an absolute address offset.

If I do this:

p1 = (int*)(p2+offset);

no warning and types are compatible but the value 15 would be changed
to 60 because C would change it from a integer to a pointer (4 bytes
long in this case). This is not want I wan.

If I do that:

This problem arises when I program my code using a Cross Code C
compiler. The code is a testcase of a hardware interface card.

Any help would be apprecciated!!

--
Ken Chan
Department of Computer Engineering, University of Alberta
Edmonton, Alberta, Canada



Thu, 06 Mar 1997 03:05:22 GMT  
 Puzzle on type casting an integer

Quote:

>HI, I have the following problem:
>int *p1;
>int *p2;
>int offset=15;
>p1 = p2 + offset;
>>> of course , ANSI C complains that type mismatch or incompatible
>between the right and left side.

Oh does it? What compiler are you using? Maybe you should use an
ANSI C compiler :-)

The statement

 p1 = p2 + offset;

should not lead to any complaints from an ANSI C compiler.
If your's gives a diagnostic for that statement, get a new one.

Quote:
>what I want to do here is that I want
>to assign an absolute memory address to p1. This address is the sum
>of another address and an absolute address offset.
>If I do this:
>p1 = (int*)(p2+offset);

This is exactly the same as the above. There is no difference but
the additional effort in typing.

Quote:
>no warning and types are compatible but the value 15 would be changed
>to 60 because C would change it from a integer to a pointer (4 bytes
>long in this case). This is not want I wan.

If p1 is a valid pointer to int, and if you do

  p2 = (int *)(((char *)p1) + 15);

then the results of using p2 as an int pointer are "intersting"
on a lot of machines.

Why don't you use char pointers?

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-2158




Thu, 06 Mar 1997 04:15:31 GMT  
 Puzzle on type casting an integer

Quote:

>HI, I have the following problem:

>int *p1, *p2, offset=15;

>p1 = p2 + offset;

>what I want to do here is that I want
>to assign an absolute memory address to p1. This address is the sum
>of another address and an absolute address offset.

>If I do this:

>p1 = (int*)(p2+offset);

>no warning and types are compatible but the value 15 would be changed
>to 60 because C would change it from a integer to a pointer (4 bytes
>long in this case). This is not want I want.

        Let's restate the problem slightly:

        (1) You want p2 to point to a specific memory location,
            the address of an int:

                p2 = &i;     /* for some int i */

        (2) You want p1 to point 15 BYTES past p2, and you want
            this to be treated as the address of an int.

        Problem is, as you've noted (though incorrectly stated
        the reason) somehow 60 is getting added to p2 instead of
        15.  That's because in C, adding an integer to a pointer
        is done in units of the type the pointer points to; i.e.,
        p1 + 15 is essentially computed as p1 + 15*sizeof(int).
        We need to convince the compiler that p1 is a pointer to
        char:

                p1 = (char *)p2 + offset;

        Now, however, p1 is pointing to a place that is almost
        certainly not an integer, but at least the statement
        does what you claimed you wanted.

Larry Miller            The Aerospace Corporation

310-336-5597            LA, CA 90009-2957



Sat, 08 Mar 1997 01:25:14 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. cast cstring type to integer type

2. Type casting string to integer

3. Casting a 32-bit unsigned integer to a 16-bit unsigned integer

4. Type Casting between Base and Derived Type (Serialization of objects with Type members)

5. Newbie very puzzled - no cast required??

6. type casting to / from pointers to incomplete data types

7. type casting to / from pointers to incomplete data types

8. Casting to/from STL types to MFC types

9. Why can't overload type cast to base type (object)

10. Jumble type puzzles

11. Inheritance in enumerated types puzzle

12. q: Casting array of bytes into integer (ANSI C)

 

 
Powered by phpBB® Forum Software