0

**Hey guys i'm having trouble casting my void pointer array to point to my float array, i want my void pointer to point to the array of floats so void[0]=float[0] i know that a void ptr and float are not the same size so i cannot assign it in this manner void[0]=float[0] how do i tackle this issue? Thanks!

void *ptr[5];
float arr[5];
for(i=0;i<5;i++)
ptr[i]=arr[i];

How do i fix this issue? i want to send any array using a void pointer array I have added the code:

#define _CRT_SECURE_NO_WARNINGS
#define N 5
#include <stdio.h>
#include <stdlib.h>
typedef enum {FALSE,TRUE} BOOL;
BOOL Int_Sum(void* a, void* b, void* c)
{
    if (*(int*)a + *(int*)b == *(int*)c)
        return TRUE;
    return FALSE;

};
BOOL Float_sum(void*a, void* b, void* c)
{
    if (*(float*)a + *(float*)b == *(float*)c)
        return TRUE;
    return FALSE;

};
BOOL Sum(BOOL(*F)(void*, void*, void*), void** p_num, void* number)
{
    int i = 0,j=0;
    for (i = 0; i <N; i++)
    {
        for (j = 0 ; j <N; j++)
        {
            if (j != i)
            {
                if (F(&p_num[i], &p_num[j], number))
                    return TRUE;
            }


        }
    }
    return FALSE;

}
int main()
{
    int num[] = { 3,5,23,5,6 }, i=0, value;
    float fnum[] = { 3.5,5.0,2.3,5.8,6.2 }, fvalue;
    void* p_num[N];
    float* f_pnt[N];
    BOOL* fpnt;
    fpnt=Int_Sum;
    for (i = 0; i < N; i++)
        p_num[i] = num[i];
    printf("\nPlease enter an integer number:");
    scanf("%d", &value);
    if (Sum(fpnt, p_num,&value )== TRUE)
        printf("There is such sum\n");
    else
        printf("There is no such sum\n");
    printf("\nPlease enter an integer number:");
    scanf("%f", &fvalue);
    fpnt = Float_sum;
    for (i = 0; i < N; i++)
    {
        (float*)p_num[i] =fnum+i;
    }
    if (Sum(fpnt, p_num, &fvalue))
        printf("There is such sum\n");
    else printf("There is no such sum\n");



    return 0;

}

Having trouble with the funciton when i want to use the float array

**

Soske
  • 541
  • 1
  • 4
  • 10
  • What makes you say void pointers and float pointers are not the same size? – Scott Hunter May 26 '20 at 13:44
  • 2
    Surely `void*` and `float` could be different sizes, and it doesn't make sense to assign one to the other. But that's what you're doing here. Did you intend `ptr[i] = &arr[i]`? – Fred Larson May 26 '20 at 13:45
  • 2
    What do you mean by "send any array"? What are you actually trying to accomplish here? – Scott Hunter May 26 '20 at 13:47
  • Does this answer your question? [void\* is literally float, how to cast?](https://stackoverflow.com/questions/15313658/void-is-literally-float-how-to-cast) – Abhishek Bhagate May 26 '20 at 13:50
  • 2
    Well, i could have explained myself alot better, i have a function which intakes a array of void pointers and checks to see if there are identical things in the array. i have 2 arrays in my main, one of them is int , and the other one is float. i need to ASSIGN THE VOID POINTERS to point to the FLOAT ELEMENTS so Void ptr[0]=float arr[0]. how do i accomplish this? – Soske May 26 '20 at 13:50
  • `(float*)p_num[i] =fnum+i;` --> `p_num[i] =fnum+i;` – chux - Reinstate Monica May 26 '20 at 14:11
  • This did not work, sadly i still get a wrong answer when i send the number 11.2 into the function (float function) – Soske May 26 '20 at 14:22
  • @Soske You are incorrect, `11.2` should fail due to the binary nature of floating point. Print each `float` with using `"%.20f"` to see why. – chux - Reinstate Monica May 26 '20 at 14:25
  • I'm afraid you are wrong... a `void *` and a `float *` are exactly the same size. The pointed thing is what is different, but not the pointer. BTW, why do you play with `void` pointers? That's like playing russian roulette by pleasure. Please, don't involve us on that !!! 8) – Luis Colorado May 27 '20 at 12:02

1 Answers1

1

How do i fix this issue?

Save time. Enable all compiler warnings. That is how I found most code issues here.


At least these problems:

Wrong type declaration for function pointer

An object pointer BOOL *fpnt is not sufficient to certainly store a function pointer. Use a function pointer.

//BOOL *fpnt;
BOOL (*fpnt)(void*, void*, void*);

Wrong argument types in F()

    // if (F(&p_num[i], &p_num[j], number))
    if (F(p_num[i], p_num[j], number))

Casting hints something is wrong

Cast causes code to fail to compile. Cast not needed.

// (float*)p_num[i] =fnum+i;
p_num[i] = fnum+i;

Assigning an int to a pointer

Instead, assign the address of the int.

for (i = 0; i < N; i++)
  // p_num[i] = num[i];
  p_num[i] = &num[i];

i know that a void ptr and float are not the same size so i cannot assign it in this manner void[0]=float[0]

Assigning a float to a pointer makes little sense.

Assigning a float * to void *: It is also true that float * and void * may differ in size and encoding, yet that is not the key problem here.


Note that values like 2.3,5.8,6.2 are not encoded exactly as float. Instead nearby values are used. Code might not behave as hoped.

Add below to see why.

BOOL Float_sum(void *a, void *b, void *c) {
  printf("%.20f %.20f %.20f  %.20f\n", 
      *(float*) a, *(float*) b, *(float*) c, *(float*) a + *(float*) b);
  ...
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • All works ! Could u care to explain why int assign is p_num[i]=&arr but for float its p_num[i]=arr+i Thanks alot! – Soske May 26 '20 at 14:55
  • @Soske There is no `p_num[i]=&arr` or `p_num[i]=&of_some_array_object` . Question unclear or imprecise. – chux - Reinstate Monica May 26 '20 at 15:12
  • Why do i assign void *arr to int arr with an &, but when i assign void * to float arr with arr+i; – Soske May 26 '20 at 15:20
  • @Soske Please copy the line of code here as text and apply the question to that line. – chux - Reinstate Monica May 26 '20 at 15:24
  • Assigning Void *arr[5] to int arr[5] i use this code : for (i = 0; i < N; i++) p_num[i] = &num[i]; But when i assign Void *arr[5] to float arr[5] i use this : for (i = 0; i < N; i++) { p_num[i] = fnum+i; } – Soske May 26 '20 at 15:34
  • `p_num[i] = &num[i];` is equivalent to `p_num[i] = num + i;` and `p_num[i] = fnum+i;` equivalent to `p_num[i] = &fnum[i];`. Two different styles to do the same thing. No significant difference. The original flawed code was corrected using the most similar style. – chux - Reinstate Monica May 26 '20 at 15:40
  • Got it , i appreciate the help bro!! – Soske May 26 '20 at 16:06