Bei
Bei

Reputation: 95

Can I access a member of a struct with a pointer to a struct which contains a pointer to that struct

I am trying to access the members in the struct tCAN_MESSAGE. What I think would work is like the first example in main, i.e. some_ptr->canMessage_ptr->value = 10;. But I have some code that someone else have written and what I can see is that that person have used some_ptr->canMessage_ptr[i].value;.

Is it possible to do it the first way? We are using pointers to structs which contains pointer to another struct (like the example below) quite often, but I never see the use of ptr1->ptr2->value?

typedef struct
{
     int value1;
     int value2;
     int value3;
     float value4;
}tCAN_MESSAGE;

typedef struct
{
     tCAN_MESSAGE *canMessage_ptr;
}tSOMETHING;

int main(void)
{
    tCAN_MESSATE var_canMessage;
    tSOMETHING var_something;

    tSOMETHING *some_ptr = &var_something;
    some_ptr->canMessage_ptr = &var_canMessage;


    some_ptr->canMessage_ptr->value1 = 10; //is this valid?

     //I have some code that are doing this, ant iterating trough it with a for:
    some_ptr->canMessage_ptr[i].value1; //Is this valid?

    return 0
}

Upvotes: 1

Views: 113

Answers (2)

John Bollinger
John Bollinger

Reputation: 181149

I am trying to access the members in the struct tCAN_MESSAGE. What I think would work is like the first example in main, i.e. some_ptr->canMessage_ptr->value = 10;. But I have some code that someone else have written and what I can see is that that person have used some_ptr->canMessage_ptr[i].value;. Is it possible to do it the first way?

The expression

some_ptr->canMessage_ptr[i].value

is 100% equivalent to

(*(some_ptr->canMessage_ptr + i)).value

, which in turn is 100% equivalent to

(some_ptr->canMessage_ptr + i)->value

. When i is 0, that is of course equivalent to

some_ptr->canMessage_ptr->value

So yes, it is possible to use some_ptr->canMessage_ptr->value as long as the index in question is 0. If the index is always 0 then chaining arrow operators as you suggest is good style. Otherwise, the mixture of arrow and indexing operators that you see in practice would be my style recommendation.

We are using pointers to structs wich contains pointer to another struct (like the example below) quite often, but I never see the use of ptr1->ptr2->value ?

I'm inclined to suspect that you do not fully understand what you're working with. Usage of the form some_ptr->canMessage_ptr[i].value suggests that your tSOMETHING type contains a pointer to the first element of an array of possibly many tCAN_MESSAGEs, which is a subtle but important distinction to make. In that case, yes, as shown above, you can chain arrow operators to access the first element of such an array (at index 0). However, the cleanest syntax for accessing other elements of that array is to use the indexing operator, and it pays to be consistent.

Upvotes: 1

Lundin
Lundin

Reputation: 214515

It's very simple: every pointer has to be set to point at a valid memory location before use. If it isn't, you can't use it. You cannot "store data inside pointers". See this:

Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer

None of your code is valid. some_ptr isn't set to point anywhere, so it cannot be accessed, nor can its members. Similarly, some_ptr->canMessage_ptr isn't set to point anywhere either.

Upvotes: 2

Related Questions