user2665457
user2665457

Reputation: 13

C defining function pointer with pointer agruments

I'm trying to understand what's wrong with:

typedef *void (*pfun)(**int, *float);

as far as I understand the problem is that I can't pass the function the pointers as

typedef *void (*pfun)(int, float);

doesn't generate an error but I have no idea why that is the case.

Upvotes: 0

Views: 192

Answers (4)

JackCColeman
JackCColeman

Reputation: 3807

This is a good question for new programmers. The de-reference operator has several uses in defining pointers.

First, it can be placed BEFORE a variable name. This implies that the variable is a pointer to a data type, such as: int *X; means that X points to RAM which contains an integer value.

Second, it can appear to stand alone as part of a cast statement: (int *) Y which means that the contents of Y are to be interpreted as a pointer to an integer.

Third, and probably its most obtuse usage is to indicate a pointer to a function. For example,

int (*func_ptr_ret_int)(void);

Declares to C that the variable func_ptr_ret_int points to a function that does NOT take any parameters and returns an integer. In this example, func_ptr_ret_int has yet to be assigned a value and so it should be assumed to contain garbage.

A fourth usage, is to indicate a double pointer: int **Z; declares that Z points to a another pointer(un-named) which in turn points to an integer.

Finally, I would recommend that you defer using typedef statement until you can code a declaration "natively". That is, typedef only defines a new data type is does NOT allocate storage or assign values.

With this in mind your two statements could be coded as:

void (*pfun1)(int **, float *); // pfun1 points to a function that returns void
                                // and uses a double ptr to inf and ptr to float


void *(*pfun2)(int, float);     // pfun2 points to a function that returns a void
                                // pointer and accepts int and float parameters.

Hope this helps.

Upvotes: 0

Jacob Pollack
Jacob Pollack

Reputation: 3751

You are not using valid C/C++ syntax for pointer declaration in the following expression:

typedef *void ( *pfun )( **int, *float );

Recall: Points are declared in the following format:

datatype *identifier

... and hence your type definition should be written as:

typedef void* (*pfun)( int**, float* );

Remark: Spacing does not matter when declaring pointers, hence the following are equivalent:

datatype *identifier
datatype* identifier

... however you will find that most programers agree that it is a good practice to do the first pointer declaration as it communicates that the identifier is a pointer to a data type. This practice becomes more apparently useful when declaring multiple pointer on one line. Example:

int *ptr1, *ptr2, *ptr3; // Declaring 3, valid, pointers to integers.
int* ptr1, ptr2, ptr3; // Declares 1, valid, pointer to an integer and 2 other integers.

Upvotes: 1

Knight71
Knight71

Reputation: 2949

typedef void* (*pfun)(int **i, float f); This means function pointer which has int * , float * as arguments and returns void *. But in your post the dereference operator is not in proper place.

Upvotes: 0

Uchia Itachi
Uchia Itachi

Reputation: 5315

Did you mean void* , int** and float*?

Upvotes: 1

Related Questions