- Joined
- Jun 2, 2022
- Messages
- 1
- Reaction score
- 0
Hi, Im new to C programming. Im am writing a function that takes as input
an array filled with a custom datatype as a void pointer,
the number n of elements inside this array,
its size,
the number of its size to use for pointer arythmetic
and a function pointer of another function func2 that takes two different elements of the array
and performs some action with them.
typedef is declared with 3 int values.
custom_type arr[]={
{2,3,4}
{4,6,1}
…
}
void func1(void*Arr, n , size ,int (*func2)(void*,void*)){
func2(element_of_arr_1, element_of_arr_2)
}
Im trying to pass the pointers to element 1 and 2 which are typedef to func2. However, I cant seem to access the adress of any element other than the first one and pointer arithmetic fails , I tried to add the value of the first element of Arr to arr itself like
arr+*arr
that didnt work because its a void pointer , I tried to typecast it to get it to use the correct type to add its size to the adress like
arr+((custom_type*)arr)
just like I would do
arr+1 if it was of type int, but this doesnt work either. I can’t figure out how to get it to be the adress of the 2nd element.
What is the correct way to get the adress pointer of the second element of arr? what is the right way to add to this typedef pointer adress so that it results in the adress of the next index?
an array filled with a custom datatype as a void pointer,
the number n of elements inside this array,
its size,
the number of its size to use for pointer arythmetic
and a function pointer of another function func2 that takes two different elements of the array
and performs some action with them.
typedef is declared with 3 int values.
custom_type arr[]={
{2,3,4}
{4,6,1}
…
}
void func1(void*Arr, n , size ,int (*func2)(void*,void*)){
func2(element_of_arr_1, element_of_arr_2)
}
Im trying to pass the pointers to element 1 and 2 which are typedef to func2. However, I cant seem to access the adress of any element other than the first one and pointer arithmetic fails , I tried to add the value of the first element of Arr to arr itself like
arr+*arr
that didnt work because its a void pointer , I tried to typecast it to get it to use the correct type to add its size to the adress like
arr+((custom_type*)arr)
just like I would do
arr+1 if it was of type int, but this doesnt work either. I can’t figure out how to get it to be the adress of the 2nd element.
What is the correct way to get the adress pointer of the second element of arr? what is the right way to add to this typedef pointer adress so that it results in the adress of the next index?