Function Prototypes


Very new to c !!
I am confused about these c++ statements.


First I define a function protype as:

typedef BOOL WINAPI PrototypeOfTargetFunction
(
DATA_BLOB *pDataIn,
LPWSTR *ppszDataDescr,
DATA_BLOB *pOptionalEntropy,
PVOID pvReserved,
CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct,
int dwFlags,
DATA_BLOB *pDataOut
);


Q1) what is the meaning of *pDataIn?
is it that pDataIn is a pointer to a DataBlob
then what is *pDataIn ?




PrototypeOfTargetFunction* fn = (PrototypeOfTargetFunction*)Ptr_TargetFunction;

Q2) What does the above do?
Does it say that 'fn' is a function with the same signature as the prototype and that fn is located at the address of Ptr_TargetFunction.
Why the *'s in the above?


BOOL RV = (*fn)(pDataIn,ppszDataDescr,pOptionalEntropy,pvReserved,pPromptStruct,dwFlags,pDataOut);

Q3) why (*fn) in the calling statement?
Q4) why no * in the arg list? why pDataIn and not *pDataIn in the arg list?

I am confused about * in these situations.

My basic understanding is that *pName means the contents of memory pointed to by pName.

Thanks for any help.
OK _ i'll have a first shot at this then.

Q1)what is the meaning of *pDataIn? is it that pDataIn is a pointer to a DataBlob then what is *pDataIn ?
A1) Yes the declaration DATA_BLOB *pDataIn is a pointer to type DATA_BLOB
The * in a declaration denotes that a pointer type is being declared.
so type *variablename means variablename is a pointer to type

Q2)
PrototypeOfTargetFunction* fn = (PrototypeOfTargetFunction*)Ptr_TargetFunction; What does the above do?
Does it say that 'fn' is a function with the same signature as the prototype and that fn is located at the address of Ptr_TargetFunction.
Why the *'s in the above?


This is a declaration&initialisation all in one. We can split it into 2 parts as follows:
PrototypeOfTargetFunction* fn This is declaring a pointer (named fn ) to type PrototypeOfTargetFunction.
We already defined the type PrototypeOfTargetFunction earlier.
So fn is a pointer to a function that takes (
DATA_BLOB *, LPWSTR *, DATA_BLOB *, PVOID , CRYPTPROTECT_PROMPTSTRUCT *, int dwFlags, DATA_BLOB *) and returns an BOOL

the second part is:
fn = (PrototypeOfTargetFunction*)Ptr_TargetFunction
This assigns the address of the functionPtr_TargetFunction to the function pointer fn.
The cast operation (PrototypeOfTargetFunction*) just makes sure that the address of the function is of the right type to match the pointer fn.
(In this case it may be redundant).

Q3
BOOL RV = (*fn)(pDataIn,ppszDataDescr,pOptionalEntropy,pvReserved,pPromptStruct,dwFlags,pDataOut); why (*fn) in the calling statement?
This is how you call a function through a function pointer. Note you can just write it as
BOOL RV = fn(pDataIn,ppszDataDescr,pOptionalEntropy,pvReserved,pPromptStruct,dwFlags,pDataOut);

Q4. why no * in the arg list? why pDataIn and not *pDataIn in the arg list?
If you passed *pDataIn (for example) you will be dereferencing the pointer - which would be in-correct.


There are at least 3 uses of the * symbol:
1
2
3
4
5
6
7
8
9
10
11
int a,b, result, x;
a = 2;
b = 3;
result = a*b; //Multiplication;

int *p;  //pointer declaration;

p = &x;

*p = 10;  //pointer dereferencing (using the contents of the pointer)
Thank you much for your reply.

I have printed it out and will keep it as my 'bible' on the use of * in C++.

Question about your final example:

int *p; // p is a pointer to type integer;

p = &x; // p now holds the address of x

*p = 10; //pointer dereferencing (using the contents of the pointer)

so x is now 10?

Topic archived. No new replies allowed.