-1

I write:

printw("\nNow, Which is the type of data to be saved?\n");
printw("\n1- Integer\n2- Short-integer\n3- Char\n4- Float\n");
printw("\nSelect the number of the option (1-4)\n");

do{
    scanf("%d",&h);
    switch(h){
        case 1:
            int matriz[rows][columns];
            break;
        case 2:
            short int matriz[rows][columns];
            break;
        case 3:
            char matriz[rows][columns];
            break;
        case 4:
            float matriz[rows][columns];
            break;
        default:
            printw("\nYou have to enter a number between 1 and 4 :)\n");
            break;


    }
}while(h > 4 || h < 1);

(Previously I defined h, rows, colunmns and I am using ncurses)

I want to do an array of the type that user wants. But I realized that this isn't the way.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Pablo De Luca
  • 795
  • 3
  • 15
  • 29

2 Answers2

-1

Typical case of type generic programming in C. It can be done but it is a bit burdensome:

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

#define ROWS 2
#define COLS 3


typedef enum
{
  TYPE_INT,
  TYPE_SHORT,
  TYPE_CHAR,
  TYPE_FLOAT
} type_t;


typedef struct generic_t generic_t; // forward declaration, incomplete type

// generic print function type
typedef void(*print_func_t ) (const generic_t* ptr, size_t row, size_t col);

typedef struct generic_t
{
  type_t        type;   // corresponding to selected data type
  print_func_t  print_func;
  void*         matrix2D;
} generic_t;


// specific print function just for float
void print_float (const generic_t* ptr, size_t row, size_t col);


int main()
{
  generic_t data;

  // take user input etc, determine type selected

  // lets assume they picked float
  data.type = TYPE_FLOAT;
  data.print_func = print_float;
  data.matrix2D = malloc (ROWS*COLS*sizeof(float));

  float some_data[2][3] =   // some random data:
  {
    {1, 2, 3}, 
    {4, 5, 6}, 
  };
  memcpy(data.matrix2D, some_data, ROWS*COLS*sizeof(float));

  for(size_t r=0; r<ROWS; r++)
  {
    for(size_t c=0; c<COLS; c++)
    {
      data.print_func(&data, r, c);
    }
    printf("\n");
  }

  free(data.matrix2D);
}


void print_float (const generic_t* ptr, size_t row, size_t col)
{
  float(*fmatrix)[COLS] = ptr->matrix2D; // cast to a pointer to a 2D float array
  printf("%f ", fmatrix[row][col]); // treat data as 2D array of float
}

You can create similar functions depending on what you want to do with the data, one function for each possible type.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-1

As said in the comments, if you only wish to use matriz in each case independently from the others, you just have to put each case's body inside its own block with { ... }. If on the contrary you want to access matriz in the whole body of your function, you have to declare it before the switch, and with a type that encompasses all possible uses. This is what unions are for.

In the following example, enum kind is used to recall the choice of the user, while union container can store any of your four choices. Then, in the main function, based on the value of k, you can decide which type of value must be in the cells of matriz, and use the appropriate field of union container to access them.

#include <stdio.h>

const int rows = 3;
const int columns = 3;

enum kind { INT, SHORT, CHAR, FLOAT };
union container { int ival; short sval; char cval; float fval; };

int main() {
enum kind k;
union container matriz[rows][columns];
int h;
do{
    scanf("%d",&h);
    switch(h){
      case 1:
          k = INT;
          break;
      case 2:
          k = SHORT;
          break;
      case 3:
          k = CHAR;
          break;
      case 4:
          k = FLOAT;
          break;
      default:
        printf("\nYou have to enter a number between 1 and 4 :)\n");
        break;
    }
 } while(h > 4 || h < 1);

 if (k == FLOAT) matriz[0][0].fval = 0.0;
 if (k == INT) matriz[0][0].ival = -1;
 if (k == SHORT) matriz[0][0].sval = 2;
 if (k == CHAR) matriz[0][0].cval = 'a';

 return 0;
}
Virgile
  • 9,724
  • 18
  • 42