Soske
Soske

Reputation: 583

Trouble sending a float array using void pointers

**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

**

Upvotes: 0

Views: 374

Answers (1)

chux
chux

Reputation: 153547

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);
  ...

Upvotes: 1

Related Questions