0

I am having lot of problems with my C code. I am saving data into my array with this function:

/**
* Insert Data into array
* @param char[] pointer
*/
void insertData(char **data) {
    int i;

for (i = 0; i < 2; i++) {
      data[i] = malloc(10000);
      printf("Nombre del paciente %d: ", i+1);
      scanf("%s", &data[i][0]);
      printf("Habitacion: ");
      scanf("%s", &data[i][1]);
      printf("Cama: ");
      scanf("%s", &data[i][2]);
      free(*data);
    }
}

My variable data is : char data[2][3];

And I'm trying to show this data with the next function:

void mostrarResultados(char **data) {
int i,j;
for (i = 0; i < 2; i++) {
    printf("\n");
     for (j = 0; j < 3; j++) {
        printf("%c ", data[i,j], **data);
     }
    }
}

But the console return me weird characters: enter image description here

What am I doing wrong?

To these functions I am calling them this way:

    //First menu
    do{
        switch (option){
            case 1:
                insertData(data);
                setFirstTime(false);
                mostrarResultados(data);
                break;
            case 2:
                exit(0);
                break;
        }
    } while (option == 0);
} 

Thank you very much for your help!

2 Answers 2

1

I think you better can use a struct to hold the data. Someting like

Struct data {
   char nombre[32];
   Char habitacion[32];
   char cama[32];
}

And malloc the struct malloc(sizeof(struct data) * nr of struct you want)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but I have to declare 20 structs new, because I am doing an example with an array[2] but in the future i want to do with 20, and I have to do 20 struct Data Data1; etc...
But you can make an array of structs
0

When programming in C or C++, you must enable compiler warnings, understand them and fix them properly. Everything else is irresponsible.

In this case, the compiler will warn about the printf call, since you are passing the wrong type (and number) of arguments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.