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

**

9
  • What makes you say void pointers and float pointers are not the same size? Commented May 26, 2020 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]? Commented May 26, 2020 at 13:45
  • 2
    What do you mean by "send any array"? What are you actually trying to accomplish here? Commented May 26, 2020 at 13:47
  • Does this answer your question? void* is literally float, how to cast? Commented May 26, 2020 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? Commented May 26, 2020 at 13:50

1 Answer 1

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);
  ...
Sign up to request clarification or add additional context in comments.

7 Comments

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 There is no p_num[i]=&arr or p_num[i]=&of_some_array_object . Question unclear or imprecise.
Why do i assign void *arr to int arr with an &, but when i assign void * to float arr with arr+i;
@Soske Please copy the line of code here as text and apply the question to that line.
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; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.