Hello, I'm new to the forums, and my knowledge of c++, is very limited. But I was looking up information regarding pointers and address-of operators (&) and was wondering if this dealt with the objects I was looking at.
I'm using an Arduino uno with an adafruit NFC shield, and within the code there is a function called readPassiveTargetID.
The following link shows the parameters that are passed ( or retrieved?) from the IS014443A card.
The example code they give has the following format:
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
So I was wondering why they had &uidLength rather than just uidLength. Also 'uid' works just find instead of &uid[0].
When &uidLength is changed to uidLength I get the error:
Invalid conversion from 'uint8_t' to 'uint8_t*'
TLDR; I would just like to know the difference between uint8_t and the one with the asterisk at the end.
Okay so then this leads to the question; what value (variable) is uint8_t* pointing to? The logic eludes me as to what is actually going on here.
I understand that the uidLength is being stored in the char byte uidLength variable but how is this happening?
Is it right to assume that uint8_t *uidLength parameter is telling the function to point the value from the read card to the address of uidLength by way of inputting '&uidLength'?
When you have an array, the variable is actually a pointer.
so This creates a char variable called x
char x="y";
BUT This reserves some space for 11 chars creates a char pointer called z that is initialised to point to that array
char z[11]="0123456789";
so if you have function that wants a char pointer and had implemented these two examples, you could pass
either &x (the address of the character 'y')
or
z (this is already holding the address of the character '0' so we don't need the &
Okay Thank you all for your replies, I was checking the Arduino site, and I was trying to look up how to reference and deference variables and it doesn't really have any information.
I tried making my own code to use the & referencing operator but didn't have any luck. So I'm assuming the x = &var, type of syntax doesn't work so simply using the Arduino sketch compiler? Is it correct to assume so?
NitroInferno:
I tried making my own code to use the & referencing operator but didn't have any luck. So I'm assuming the x = &var, type of syntax doesn't work so simply using the Arduino sketch compiler? Is it correct to assume so?
No I would not assume that. But to ask properly you need to post code that demonstrates what you are talking about.
I'll make one for you:
void setup ()
{
int var = 42; // some variable
int * x = &var; // a pointer to that variable
Serial.begin (115200);
Serial.println ();
Serial.println (*x); // prints what x is pointing to
} // end of setup
void loop () { }
Ahh okay I see thanks Nick, what I was trying to do was somehow Serial.print the address of my variable to see if the &var syntax was working sort of like this:
void setup ()
{
int var = 42; // some variable
int * x = &var; // a pointer to that variable
Serial.begin (115200);
Serial.println ();
Serial.println (&var); // prints what x is pointing to
} // end of setup
void loop () { }
I wanted to see if it could return the address of where my variable was stored(haven't been able to figure that out).
Throws an error: call of overloaded 'println(int*)' is ambiguous
It's not particularly meaningful to cast an address (pointer) to a double. After all, an address is to an integral location, you don't get an address of 42.4321.
So I was wondering why they had &uidLength rather than just uidLength.
Back to your original question, the reason for that is that they wanted to return a length to the calling function.
By way of quick example, this code:
void foo (int a)
{
a = 20;
}
void loop ()
{
int x = 42;
foo (x);
Serial.println (x); // will print 42
}
This is because "x" is passed by value, not by reference, in other words foo gets a copy of x, and thus cannot change it in a way that affects the calling function.
Meanwhile:
void foo (int * a)
{
*a = 20;
}
void loop ()
{
int x = 42;
foo (&x); // pass a pointer to x
Serial.println (x); // will print 20
}
Now by passing a pointer to x, the called function (foo) can alter it by dereferencing the pointer.
There is an alternative, and that is to pass by reference, like this:
void foo (int & a)
{
a = 20;
}
void loop ()
{
int x = 42;
foo (x);
Serial.println (x); // will print 20
}