Julia Lindell
Julia Lindell

Reputation: 61

What do the symbols -> achieve in a line of code?

The symbols -> appear in a line of code that I am trying to understand and someone told me it is a pointer.

     *data = (uint8_t)base->FIFORD;

My interpretation is that the 8 bit integer pointer base points to a register called FIFORD. The value in FIFORD is then assigned to the pointer data. Is this correct?

BONUS: If I wanted to store the values from FIFORD in a buffer to print, what would I need to do?

Thanks much!

Upvotes: 0

Views: 57

Answers (2)

Garo
Garo

Reputation: 1520

  • base is a pointer to a datastructure. A data structure just means that it's just a group of variables. They are stored as a group because they are strongly related. Somewhere else in your code base is created as a data structure of a type "X". Even before that there will be a definition of how this type should look (at the very least this definition will say that X contains a variable always called FIFORD).
    We know that base is a pointer because of the arrow. If it would be the data structure itself the code would become *data = (uint8_t) base.FIFORD;

  • FIFORD is a variable in the data structure that base points to. The code does not mention what the the type is, but what what we do know is that it's probably a number.

  • We know that it's probably a number because we use (uint8_t) to change it's type to uint8_t (which is usually used to save natural numbers)

  • data is pointer. We know this because of the *-sign. *data = means 'the place in memory that data points to should be set to...`

So the full meaning of this line:
Don't change the place data points to, but instead change the value at that spot to a uint_8t conversion of the value of the FIFORD variable in the datastructure that is saved at the spot where base points to.

PS: The original FIFORD never changes.

About the "bonus"-question, I don't really understand your question since the value is already stored in memory. (Twice even after this line). You can just use printf-funtion to print it.
Something like printf("Current value of data: %ui\n", *data); should do the trick.

Upvotes: 0

burt
burt

Reputation: 21

In your example, "base" is a pointer to a structure.

There are two ways you can access variables in a structure with its pointer (base):

  1. (*base).FIFORD
  2. base -> FIFORD

Both do the same thing

In this case, the FIFORD variable in the struct is extracted, casted to uint8_t and assigned to a, hopefully (Too little context to say), uint8_t pointer called data.

Upvotes: 1

Related Questions