How to set a variable to the same amount of space be allocated and pointed to by another pointer?

58 views Asked by At

I have an int type variable i and a float type pointer f.

How can I set i to the same amount of space be allocated and pointed to by f?

This is how I define f

float *f = NULL;
f = (float *)malloc(sizeof(float));
*f = 30.5;
1

There are 1 answers

5
Michał Marszałek On

If I understand you want to:
Edit based on new data from question.

#include <stdio.h>
#include <stdlib.h> 

int main(void) {
// your code goes here
    float *f = NULL;
    int *i = NULL;
    void* allocated_space = malloc(sizeof(float) > sizeof(int) ? sizeof(float) : sizeof(int));
    f = (float *)allocated_space;
    i = (int *)allocated_space;
    *f = 30.5;
    free(allocated_space);
return 0;
}