Reputation: 402
I am using an implementation of an UDP protocol that sends a socket like a char *. As well I have defined a struct to fit the header and body of a socket by size. Just like:
#pragma once
typedef struct WHEATHER_STRUCT{
uint8_t packetID; // 9
uint16_t packetSize; // 11
uint8_t subpacketID; // 0
uint16_t subpacketOffset; // 0
uint8_t reserved; // 0
float cloudLayerAltitude; // 25000
}
I checked the size of this socket and it is 11 bytes.
I am improving my knowledge of pointers and I am trying to access to this variables using a pointer with an offset. There is an example:
Initial position of the struct: &(this->wheather_struct[0])
I tried to use this memory address to get the value of the variable packetID. But this not work for me.
After get this solution, I will define an offset to access next value with this sentence:
&(this->wheather_struct[0]) + 1 // Memory address of the next byte?
I have the memory address of the first element of the struct but I can not get the value of it. Could you help me please?
If you could give me an example to access the value of cloudLayerAltitude using pointers with an offset could be awesome. Thank you.
Upvotes: 0
Views: 69
Reputation: 409196
If this->wheather_struct
is an array (or a pointer to the first element of an array) of WHEATHER_STRUCT
structures, then it will look something like this in memory:
+--------------------+--------------------+--------------------+-----+ | wheather_struct[0] | wheather_struct[1] | wheather_struct[2] | ... | +--------------------+--------------------+--------------------+-----+
A pointer to weather_struct[0]
will point to the first element of that array.
If you add one to that pointer you will point to the second element of that array. Not the second byte in the structure.
To get a pointer to the second byte you need to treat the pointer &weather_data[0]
as a pointer to bytes.
Upvotes: 1