Corelation
Corelation

Reputation: 79

Alias address of struct array as int

struct test
{
    char member1;
    char member2;
};

int main(void)
{
    struct test structure[] = {'h', 'i'};
    static void* p = &structure;

    printf("%i", *((int*)p));

    return 0;
}

We all know that structure should point to the address of the first element in the struct. Why by dereferencing it like that, it returns the address itself instead?

Upvotes: 2

Views: 235

Answers (3)

Gyapti Jain
Gyapti Jain

Reputation: 4106

Your code exhibits an undefined behaviour.

You are casting a structure * to type int * and dereferencing. When (mostly) the size of int is more, you are accessing offbound memory. (This is one reason, there are a few more)

+---+---+---+---+
| h | i | ? | ? |
+---+---+---+---+
Print as int: ? ? i h if machine is little endian

In ascii coding
+---+---+---+---+
| 68| 69| ? | ? |
+---+---+---+---+
Print as int: ?? ?? 69 68

Live code here

Correct usage of structures is:

struct test s1   = {'h', 'i'};
struct test s2[] = { {'t', 'o'}, {'d', 'o'} };

printf("s1   : %c, %c\n", s1.member1, s1.member2);
printf("s2[0]: %c, %c\n", s2[0].member1, s2[0].member2);
printf("s2[1]: %c, %c\n", s2[1].member1, s2[1].member2);

Upvotes: 5

M.M
M.M

Reputation: 141618

This causes undefined behaviour due to violation of the strict aliasing rule.

When reading via the expression *(int *)some_pointer , it is only valid in the following cases:

  • some_pointer points to an object declared with type int or unsigned int (optionally with const or volatile qualifiers).
  • some_pointer points into dynamically-allocated storage that has previously had an int or unsigned int written to exactly that location.

Some compilers may optimize according to this rule. For more information about the benefits of this, see here

Even if your char array had at least sizeof(int) bytes in it, the compiler might still ignore (or worse!) your attempt to alias those as an int. Despite this fact it's not uncommon in "real" code for people to try this, which is why a lot of existing code has to be compiled with -fno-strict-aliasing.

NB. There may also be undefined behaviour if structure is not correctly aligned for int.

Upvotes: 1

Ginu Jacob
Ginu Jacob

Reputation: 1798

Here you have an array called structure. Its name gives the address. So you don't need to have & operator before it.

You can have either p = &structure[0]; or p = structure;

Upvotes: 0

Related Questions