I'm trying to create an array of pointers which is to be used as an array of different data types.
Here, I've created the array arr
to hold 5 pointers, each of which points to a different variable. a
is an int
, b
is a float
, c
is a char
, d
is a string
, fun
is a function
.
#include <stdio.h>
#include <float.h>
#include <string.h>
void fun(){
printf("this is a test function");
}
int main() {
int *arr[5];
int a = 10;
float b = 3.14;
char c = 'z';
char d[] = "hello";
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
arr[3] = &d;
arr[4] = &fun;
printf("a : %d\n", *arr[0]);
printf("b : %d\n", *arr[1]);
printf("c : %d\n", *arr[2]);
printf("d : %d\n", *arr[3]);
*arr[4];
return 0;
}
I'm trying to get it to print out the values of a
, b
, c
, and d
, then execute the function fun
. However, I'm getting errors like:
warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
arr[1] = &b;
for arr[1]
, arr[2]
, arr[3]
, and arr[4]
.
Clarification: I'm trying to make an array (or, after all the comments and answers so far, an object type like struct or union) able to hold objects of different data types, including variables, functions, structs, etc. The data types are assumed to be known. I simply want to store different data types and use them in 1 object, like an array.