Reputation: 1542
I have a function (specifically a system call) that returns the result of the function that needed to be called.
The code looks like such
int32_t (*func_ptr)(int32_t fd, void * buf, int32_t nbytes);
func_ptr = (curr_proc->fds[fd].operations_pointer[READ]);
Where our curr_proc
is just a struct. This struct is holder a array of file descriptors (fds
) which holds a pointer to corresponding function to run (operations_pointer
). There is a array that holds the functions for each type of device in our system, and this array looks like such:
uint32_t * rtc_ops_table[4] = {(uint32_t *) rtc_open, (uint32_t *) rtc_read, (uint32_t *) rtc_write, (uint32_t *) rtc_close};
uint32_t * dir_ops_table[4] = {(uint32_t *) dir_open, (uint32_t *) dir_read, (uint32_t *) dir_write, (uint32_t *) dir_close};
Thus, when a program calls read, the corresponding function pointer is loaded into func_ptr
and then returned with its corresponding arguments.
The problem I am having is that in the line I assign func_ptr
, I get this warning from gcc:
warning: assignment makes pointer from integer without a cast
I have tried various casts such as (uint32_t *)
, (uint32_t)
, (int32_t)
, etc. I have also tried to assign function pointer as such:
func_ptr = &(curr_proc->fds[fd].operations_pointer[READ]);
But to no avail.
How would I fix this warning?
EDIT:
This is the struct for curr_prc
typedef struct {
file_array fds[8];
uint8_t file_names[8][32];
uint8_t proc_num;
...
} pcb_t;
And the struct for fas:
typedef struct file_array{
uint32_t * operations_pointer;
inode_t * inode;
uint32_t file_position;
uint32_t flags;
} file_array;
Upvotes: 0
Views: 510
Reputation: 1207
Try something like this instead (with the lines placed where they belong in your code, of course):
typedef int32_t (*my_func_ptr)(int32_t fd, void * buf, int32_t nbytes);
typedef struct file_array{
my_func_ptr operations_pointer[4];
inode_t * inode;
uint32_t file_position;
uint32_t flags;
} file_array;
my_func_ptr rtc_ops_table[4] = {rtc_open, rtc_read, rtc_write, rtc_close};
my_func_ptr dir_ops_table[4] = {dir_open, dir_read, dir_write, dir_close};
my_func_ptr func_ptr = (curr_proc->fds[fd].operations_pointer[READ]);
In this example, my_func_ptr is a type that describes your function pointers and is used for all variables containing them.
Alternately, you might want operations_pointer to be declared as a pointer:
my_func_ptr *operations_pointer;
so that you could just have it point to one of your two ops tables:
curr_proc->fds[fd].operations_pointer = rtc_ops_table;
then you could use the operations_pointer as if it were one of the ops_table arrays.
Upvotes: 1
Reputation: 180048
Function pointers and object pointers are distinct to C. You cannot convert directly between them, except that you can convert function pointers to type void *
, which is an object pointer type.
Additionally, if it is the intention that file_array.operations_pointer
should point to a dynamic array of function pointers, then you need an additional level of indirection.
Perhaps you want something like this, instead:
typedef struct file_array{
int (**operations_pointer)();
inode_t * inode;
uint32_t file_position;
uint32_t flags;
} file_array;
That declares operations_pointer
as a pointer to a pointer to a function accepting unspecified arguments and returning an int
. That would be suitable for a dynamically allocated block of function pointers. It is possible that your C compiler would complain there about missing function prototypes, and rightfully so: that form does not express the parameter types of the pointed-to functions, but that's appropriate if those functions don't all have compatible signatures.
If all the function pointers you intend to store will point to functions with the same signature, then it would be better to express that signature in the type declaration:
typedef struct file_array{
int (**operations_pointer)(int32_t, void *, int32_t);
inode_t * inode;
uint32_t file_position;
uint32_t flags;
} file_array;
If they do not all have the same signature, then they must at least have the same return type, for you cannot express a function pointer without designating the return type.
Upvotes: 0
Reputation: 230
operations_pointer in the file_array should be a functoin pointer type(func_ptr), it is uint32_t pointer!
Upvotes: 0