Hi,
I am a c beginner and I am trying to understand an expression I came across in some example code in the text book I'm using:
byte *b;
b = (byte *) &myPeople[ index];
If it wasn't for the asterisk I would interpret this as meaning the lvalue of mPeople[index] is converted to a byte data type and assigned to the pointer (b) rvalue. However what does the asterisk signify? Is it simply to indicate a pointer is involved?
Would appreciate clarification
byte *b;
Declares a pointer to a byte (not storage for a byte)
Then. For Example if myPeople is array of int
myPeople[ index];
would give you the int value of myPeople at array position index
but using the & sign tells the compilor you want the address of that value not the value
hence
b = (byte *) &myPeople[ index];
b now points to the value in myPeople[index]
As I understand it, the expression (byte *) means that whatever follows is the address of a byte, so that address is stored in the pointer b (previously declared to be a pointer to a byte). b will then point to the first byte of myPeople[index], an array element that may be of some other type.
(byte *)
This means cast this to a pointer to byte
if myPeople was an array of bytes you shouldnt need to cast it, because the compilor should know that &myPeople[index] would be a pointer to byte.
i.e this compiles
void setup() {
// put your setup code here, to run once:
byte myPeople[10];
byte *b;
b=&myPeople[2];
}
void loop() {
// put your main code here, to run repeatedly:
}
Thanks for the help, I now understand what (byte *) means