Anubhav Hanjura wrote:

> Trying to print *ip as a character when I apply post increment operator
> gives me 'D' and for pre-increment it gives me a bus error. Why?

You are using a big-endian processor (e.g. 680x0), right?

> #include<stdio.h>
> int main()
> {
>  char *c_array;
>  int *ip;
>  c_array = (char *)malloc(100);
>  strcpy(c_array,"ABCDEFHIFGHIJKLMN");
>  ip = (int *)(++c_array);
>  return 0;
> }

The value of the expression:

        *(int *)(c_array)

depends upon sizeof(int) and the processor's byte order:

        32-bit little-endian:           0x44434241
        32-bit big-endian:              0x41424344
        64-bit little-endian:           0x4847464544434241
        64-bit big-endian:              0x4142434445464748

Calling

        printf("%c", *(int *)(c_array))

will display either 'A' (0x41), 'D' (0x44) or 'H' (0x48) depending
upon sizeof(int) and the processor's byte order.

Using pre-increment addressing will attempt to read a 32-bit value
from an odd address, which will cause a bus error on processors which
require aligned memory access (80x86 doesn't; AFAIK 680x0 does).

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to