This question already has answers here:
How does dereferencing of a function pointer happen?
(5 answers)
Closed 6 years ago.
Suppose I have the following types:
typedef uint8 (*P2MyFunc)(void);
typedef struct
{
P2MyFunc ptr;
}MyStruct;
Given an instance inst of MyStruct. is there any difference at all between the following calls ?
(*inst.ptr)();
inst.ptr();
Both seem to work just fine but the first one might be prone to compiler warnings.
They mean the exact same thing according to the C Standard. You could even go a step further. The following below give the same result:
(********inst.ptr)();
inst.ptr();
Related
This question already has an answer here:
About meaning of ::* on this code [duplicate]
(1 answer)
Closed 5 years ago.
While refactoring a piece of code I came across the line below:
class Bar
{
protected:
int (Bar::* fooFunction)(float); //this line
}
I have never seen this kind of syntax before. What is this syntax and why is it used it used in C++?
It's a member function pointer.
Specifically, it's a pointer to a member function of a Bar object that takes a float argument and returns a int.
Read more here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_member_functions
What is this syntax
It declares a pointer to member function.
why is it used it used in C++?
It is used to point to non-static member functions.
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);
This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 9 years ago.
I would like to create a linked list.
For the following code:
struct sampleStruct
{
int a;
sampleStruct *next = NULL;
};
sampleStruct *sample = new sampleStruct;
What is the difference between sample.next and sample->next?
Okay to explain it in a more complete way. The most other guys had already wrote that you have to use '->' when ever you have a pointer. But you could also do this with '.', to do this you must respect the priority of the operators. We need the '*' to get the content of the pointer, but this has a lower priority than the '.', so you must write it into brackets to give it a higher priority, so when you want to do this with a '.' you have to wrote:
(*sample).next
and you see this is a complex syntax, to do this in a more easy way the '->' was introduced. With it you could write code in a more comfortable way.
So this is is equal to the example and it looks much better.
sample->next
Since sample is a pointer, there can't be any way to access a data member through . rather than the indirection operator ->. For example, this won't compile:
sample.next; // error: member reference type 'sampleStruct *' is a pointer;
// maybe you meant to use '->'?
The error actually speaks for itself.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Pointer vs. Reference
I recently started to 'relearn' c++ and encountered a simple question that i always had.
Int *intp = new int(10);
Int& intref = *intp;
intref prints as 10
*intp does so too.
Also do the prints of &intref and intp equal.
Long story short. Is the difference between & and * simply the way you access the value and adress?
Or is there a major difference in usage?
The * notation means what's being pass on the stack is a pointer, ie, address of something. The & says it's a reference.
Refer this Thread
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why cast an unused value to void?
for this C++ code:
MyClass::myFunc(int val)
{
//some code
(void)val;
//somecode
}
why may we need to cast val to void without being assigned to another variable ?
This is done to shut up the compiler, warning about an unused variable.