I have run into a piece of code that I want to understand. What does the following do?
int cache[5];
int *head(cache);
Specifically, what does the second line do? Is 'head' a pointer to the array 'cache'?
I have run into a piece of code that I want to understand. What does the following do?
int cache[5];
int *head(cache);
Specifically, what does the second line do? Is 'head' a pointer to the array 'cache'?
'head' is a pointer of type int
This is a good example:
void setup()
{
int myVar[5] = {1, 3, 5, 6, 8};
int *myPointer;
myPointer = &myVar[0];
// &myVar[0] is the address of the 1st element of myVar[5]
}
jbsound:
I have run into a piece of code that I want to understand. What does the following do?int cache[5];
int *head(cache);
Specifically, what does the second line do? Is 'head' a pointer to the array 'cache'?
this:
int *head(cache);
is the equivalent to:
int *head = &cache;
both versions declare the pointer head, a pointer to an int, and initialize it to the address of the first element of cache.
try it:
int cache[5];
int *head(cache);
*head = 5;
Serial.print(cache[0]);
Great explanation! Appreciate it.
Would the following move the address of the array to a new position 5 in the array?
head = cache + 5;
int *head(cache);
.
.
.
head = head + 5; //fifth element of array cache[]
BTW, you can test things out with the Serial.println statement.
.
jbsound:
Great explanation! Appreciate it.Would the following move the address of the array to a new position 5 in the array?
head = cache + 5;
0 + 5 = 5
int cache[5];
it would move the pointer outside the bounds of the array
Indeed it would.
Appreciate all the answers, Everyone!
BulldogLowell:
this:int *head(cache);
is the equivalent to:
int *head = &cache;
Shouldn't the line be:
_ int *head = cache;_ // No address-of operator
int *head(cache);
is the equivalent to:
int *head = &cache;
Really? How does THAT happen? I would have sworn that the first is an incorrectly formed function prototype. (but apparently the compiler accepts it...)
About ¾ the way down...
http://www.cplusplus.com/doc/tutorial/variables/
The keywords are...
constructor initialization
uniform initialization
westfw:
Really? How does THAT happen? I would have sworn that the first is an incorrectly formed function prototype. (but apparently the compiler accepts it...)
a function prototype wouldn't have an argument, just a parameter list (or void).
it is just alternate syntax...
like for example:
int myVar(17); //initialize myVar to 17
econjack:
Shouldn't the line be:_ int *head = cache;_ // No address-of operator
yes, or rather what I meant to write:
int *head = &cache[0];
Thanks Coding! It's a C++ thing. (gee, you learn something new every day!)
A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses
Thanks Coding Badly with +1.
Finally, a third method, known as uniform initialization, similar to the above, but using curly braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):
type identifier {initial_value};
For example:int x {0};