This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

regarding type casting

my program stop running while executing the following line.

(struct IFramHead *) ptr) ->length = Length;

where declaration are as bellow:

1: unsigned int Length

2: struct IFramHead
{
unsigned int length;
unsigned int index;
}

3: unsigned char *ptr;

I have use this sentex while writingcode for C51 compiler. is this problem is arises because of 8 or 16 bit processor, or there is some memory problem or stack problem.
if anybody know why this line not behave well then please reply me as soon as possible.

thanks
amit patel

  • "unsigned char *ptr;"

    Where does this pointer point?

  • What exactly does "stop running" mean, here? How did you know it stops, and how do you know it's *this* instruction it stops in?

    Did you try this in the simulator?

    Does this 'ptr' of yours point to existing memory? How do you know that?

    And, last but not least: what exactly makes you think that this line of code is a good thing to write, in the first place?

  • Your program will cause the hardware trap 'Word operand illegal access' if it tries to access a word operand at an odd address. The code line you gave us can do it if ptr corresponds to an odd address. It is your responsibility to make sure that ptr actually points to an IFramHead structure that is properly aligned in memory at a word boundary.

    - mike

  • Hello
    I want to know how we define pointer at any explicit location. My structure cointains two int type element.

    struct frame
    {
    unsigned int i;
    unsigned int j;
    }*ptr;

    When i define a pointer to this structure then it points to odd address, when i use this pointer in programm it gives folowing traps.
    'Word operand illegal access'

    Is there is any method in c166 by which this pointer points to an even address to prevent this problem.
    Reply me soon.

  • When i define a pointer to this structure then it points to odd address

    Only if the way you defined this pointer was incorrect. Until you reveal how you arrived at that pointer, and give some explanation of why you thought this method should have worked, the only sensible answer remains: "Don't do that, then!".