This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Calling a function through a function pointer - dereference the pointer or not? What's the difference?
(2 answers)
Closed 12 months ago.
what's the difference between the second and third line:
int (*myFunction)() = fooFunction;
(*fooFunction)();
fooFunction();
Why do both do the same thing? Is it just how C++'s syntax is? I thought that a function pointer needs to be dereferenced in order to call it. How does the third line work, it isn't dereferenced.
Related
This question already has answers here:
Why pass by const reference instead of by value?
(8 answers)
Which is faster? Pass by reference vs pass by value C++
(1 answer)
Performance cost of passing by value vs. by reference or by pointer?
(6 answers)
Closed 5 months ago.
When I pass a std::vector into a C++ function, like so:
void myFunc(std::vector<int> myVec)
{
// do something
}
Does the vector myVec get copied or is in reality only a pointer passed? I'm asking from optimization perspective; should I always explicitly pass a pointer to the vector or is there a difference in speed? Assume that I only want to read the data in the function, not manipulate the vector itself.
This question already has answers here:
Can I directly assign an address to a pointer? If so, how to do that?
(2 answers)
Closed 2 years ago.
i want to Convert value int (For example: 0x00AAFAD8) to pointer (Also the pointer itself is the 0x00AAFAD8 value). I don't know how it's called, so i want help for manage memory spaces by values like this.
Thanks.
You can set the pointer address this way:
int i = 0x00AAFAD8;
int* ptr = reinterpret_cast<int*>(i);
This question already has answers here:
Passing references to pointers in C++
(10 answers)
Closed 4 years ago.
I have two function signatures in C++
void printArray(int* arrayPtr);
void printArray(int*& arrayPtr);
I understand the 1st function. It says the function takes in a arrayPtr argument which is of type that's a pointer pointing to an integer.
Both function signature works, but I have a hard time understanding the 2nd signature(*&) and what benefits it offers?
It's exactly the same as type versus type&; the first is a value and the second is a reference. The fact that type is a pointer doesn't change that.
This question already has answers here:
Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
(5 answers)
How do function pointers in C work?
(12 answers)
Closed 4 years ago.
I'd like to know what is the difference between these two (seemingly correct) ways of passing a function to another function:
void fun(int p(int x));
void fun(int (*p)(int x));
Also, I know that in the second case we're passing a function pointer, but is the first approach also a function pointer (but without the star)?
This question already has answers here:
Typedef function pointer?
(6 answers)
Closed 6 years ago.
I ran across a line of code that looks like the following:
typedef Foo* (*CREATE_BAR)(uint32_t);
How exactly does this work? What is happening in this code?
It's a function pointer type named CREATE_BAR which accepts a uint32_t argument and returns a Foo*. It could hold a pointer to any such function.
It is a type for a pointer on function returning Foo*, and taking uint32_t
In c++11, it would be
using CREATE_BAR = Foo* (*)(uint32_t);