Reputation: 23
I have a func to add two nos and return (a+b). Then I created a func pointer to the func. Want to allocate memory for an array of that function pointer and access them. code is below.
My question is on the following line using malloc:
pp = (add_2nos*)malloc(5 * sizeof(add_2nos*))
sizeof(add_2nos*) and sizeof(add_2nos) does not make any difference while compiling. What is the difference if there is any ?? Also if type casting is necessary while I am allocating memory of the same type...?
#include <stdio.h>
#include <stdlib.h>
int add(int a, int b) {
return (a+b);
}
typedef int (*add_2nos)(int, int);
int main() {
add_2nos *pp;
// Defining an array of pointers to function add and accessing them
pp = (add_2nos*)malloc(5 * sizeof(add_2nos*));
pp[0] = add;
pp[1] = add;
printf("\n\nAdding two nos -- (14, 15): %d ", pp1[0](14, 15));
printf("\nAdding two nos -- (16, 16): %d \n\n", pp1[1](16, 16));
}
Upvotes: 0
Views: 810
Reputation: 149
Pointer determines memory address. The easiest way to explain this is by this example:
printf("%ld\n",sizeof(unsigned char));
printf("%ld\n",sizeof(unsigned char *));
In the first line, the size of an unsigned char is printed, which is 1 byte. In the second line, the size of a pointer which keeps the information, where unsigned char is stored in the memory. This may vary between compilers/platforms. For example, on https://www.onlinegdb.com/online_c_compiler size of 8 bytes is printed.
Upvotes: -1
Reputation: 126546
This
pp = (add_2nos*)malloc(5 * sizeof(add_2nos*));
is giving you an allocation based on the size of a pointer to a function pointer. That's probably the same as a function pointer, but maybe not.
pp = malloc(5 * sizeof(add_2nos));
or
pp = malloc(5 * sizeof *pp);
You've typedef's add_2nos
to be a typename for "pointer to function with a particular signature", so to allocate an array of such pointers, you just want to use sizeof on it directly (no dereference), or use sizeof on the dereference of the array pointer.
Upvotes: 1
Reputation: 142080
sizeof(add_2nos*) and sizeof(add_2nos) does not make any difference while compiling. What is the difference if there is any ?
add_2nos
is void (*)(int, int)
- it's a pointer to a function.
add_2nos*
is a void (**)(int, int)
- it's a pointer, to a pointer to a function.
Because on most architectures all pointers have the same size, including function pointers and pointers to pointers, and malloc()
just takes a number, it doesn't make a difference. Anyway you want to allocate space for 5
function pointers (ie. 5 * sizeof(add_2nos)
), not for 5
pointers to function pointers.
Don't think about it and let the compiler figure it out: pp = malloc(5 * sizeof(*pp));
is a common pattern.
Also if type casting is necessary while I am allocating memory of the same type...?
See do we cast the result of malloc in C.
Upvotes: 2