1

I am having a little bit of confusion about derefrencing a structure pointer to a structure variable.

It will be good if I demonstrate my problem with an example.

So here I am:

struct my_struct{
    int num1;
    int num2;
}tmp_struct;

void Display_struct(void * dest_var){

    struct my_struct struct_ptr;

    struct_ptr = *((struct my_struct *)dest_var);

    printf("%d\t%d\n",struct_ptr.num1,struct_ptr.num2);

}

int main()
{
    tmp_struct.num1 = 100;
    tmp_struct.num2 = 150;


    Display_struct(&tmp_struct);
    return 0;
}

Now when I am running this example I am able to get the code to be compiled in a very clean manner and also the output is correct.

But what I am not able to get is that is this a correct way of dereferencing the structure pointer to a structure variable as we do in case of other simple data types like this:

int example_num;

void Display_struct(void * dest_var){

    int example_num_ptr;

    example_num_ptr = *((int *)dest_var);

    printf("%d\t%d\n",struct_ptr.num1,struct_ptr.num2);
}

int main()
{
    example_num = 100;


    Display_struct(&example_num);
    return 0;
}

Here we can dereference the int pointer to int variable as it is a simple data type but in my opinion we can't just dereference the structure pointer in similar manner to a structure variable as it is not simple data type but a complex data type or data structure.

Please help me in resolving the concept behind this.

4
  • why couldn't you dereference a pointer-to-structure? what would be the point in a pointer that you can't access the underlying object of? Commented May 4, 2015 at 8:48
  • do you mean something like that: ideone.com/lduzh0 ? Commented May 4, 2015 at 8:50
  • 2
    What's the actual problem here ? BTW The second example is wrong, you try to printf struct_ptr.num1 but there is no struct_ptr in the second example. Commented May 4, 2015 at 8:59
  • struct_ptr and example_num_ptr are both not pointers. So I believe that you have a misunderstanding of what your program is actually doing. Commented May 4, 2015 at 9:07

3 Answers 3

1

The only problem is that you have to guarantee that the passed void* points to a variable of the correct struct type. As long as it does, everything will work fine.

The question is why you would use a void pointer and not the expected struct, but I assume this function is part of some generic programming setup, otherwise it wouldn't make sense to use void pointers.

However, if you would attempt something "hackish" like this:

int arr[2] = {100, 150};
Display_struct(arr);  // BAD

Then there are no longer any guarantees: the above code will compile just fine but it invokes undefined behavior and therefore may crash & burn. The struct may contain padding bytes at any place and the code also breaks the "strict aliasing" rules of C.

(Aliasing refers to the rules stated by the C standard chapter 6.5 Expressions, 7§)

Sign up to request clarification or add additional context in comments.

Comments

0

You are thinking up a problem where there isn't any. A struct-type (alias an aggregate data type) is technically not very different from any other type.

If we look at things on the lower level, a variable of any type (including a struct type) is just some number of bits in memory.

The type determines the number of bits in a variable and their interpretation.

Effectively, whether you dereference a pointer-to-int or a pointer-to-struct, you just get the chunk of bits your pointer points to.

Comments

0

In your main function, you have struct tmp_struct. It is not a pointer. But it is fine, because you pass address of tmp_struct to the function void Display_struct(void * dest_var).

Then function take the input argument, your pointer(void*). It hold the address of 'tmp_struct`.

Then inside the function you are de-referencing correctly.

struct_ptr = *((struct my_struct *)dest_var);

you deference void* to struct my_struct type. Your de-referencing correct, because you pass same type object. Otherwise it will cause run time issues.

No matter how complex your data type or data structure, de-referencing should work fine.

But if input arg type is void* make sure to pass struct my_struct to function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.