1

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
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bei
  • 95
  • 1
  • 1
  • 9

2 Answers2

2

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I did some edit on it, realized I forgot some things :) Now both pointers should contain addresses to correct structs/variable – Bei Mar 16 '20 at 11:45
  • @Bei It's an entirely different question now. Still the same principle: if a pointer points to a valid memory location, you can access the contents, otherwise you can't. – Lundin Mar 16 '20 at 11:55
1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thank you. I understand what you mean, but I can't find anywhere in the code were tCAN_MESSAGE might be declared as an array. But thanks – Bei Mar 16 '20 at 12:05
  • Another possibility is that of **very bad programming and UB** used to access the 3 variables `value1`, `value2` and `value3` as an array of integers expecting that the compiler lays out the structure with the 3 values toughtly aligned in memory. – Frankie_C Mar 16 '20 at 12:19
  • That was what I also was thinking of, but I can't imagine how they even can think of that, becuase the structure tCAN_MESSAGE contains more memeber variables than the ones I've included above, and different data-types as well. It contains some 32 bits float, 8 bits unsigned ints, a bool, and a unsgined 16 bit variable. So it seems crazy to me – Bei Mar 16 '20 at 13:15
  • @Bei, it is quite possible that the pointer points to an "array" in the sense of dynamically-allocated space that is sized for more than one element. But that's largely beside the point. – John Bollinger Mar 16 '20 at 17:40
  • Ok I found the file where the array of that structure is declared. Things makes a little bit more sense now. – Bei Mar 17 '20 at 07:28