Question

I've to read a table from a txt file and I've to write values into memory.

I'm able to read data (row by row) from file and to put them into variables (using sscanf), but I don't know how to create and fill an array of "strings".

I need to create an array of n rows and 9 cols of strings/chars.

This code gives me a compiler warning ("warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast") and the program error:

    char matrix_model_data[3][10];
    strcpy(matrix_model_data[0][0],"some text");
    printf("VALUE = %s\n",matrix_model_data[0][0]);

How can I do?

Thanks

EDIT

Now I've modified the code using my values, but it prints only the last record 1317 times (mdata_num = 1317)...why?

    char ***table = (char ***) calloc(mdata_num, sizeof(char**));

    int i, j, m;
    for(i=0; i<mdata_num; i++)
    {
        fgets(LineIn,500,fIn);
        sscanf(LineIn, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",stTemp0,stTemp1,stTemp2,stTemp3,stTemp4,stTemp5,stTemp6,stTemp7,stTemp8);
        iterazioni++;
        table[i] = calloc(COLUMNS, sizeof(char*));
        for(j=0; j<COLUMNS; j++)
        {

            table[i][j] = calloc(MAX_STRING_SIZE, sizeof(char));
        }
        table[i][0] = stTemp0;
        table[i][1] = stTemp1;
        table[i][2] = stTemp2;
        table[i][3] = stTemp3;
        table[i][4] = stTemp4;
        table[i][5] = stTemp5;
        table[i][6] = stTemp6;
        table[i][7] = stTemp7;
        table[i][8] = stTemp8;
    }

    for(i=0; i<mdata_num; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            printf("%s\t", table[i][j]);
        }
        printf("\n");
    }

    //FREE THE TABLE
    for(i=0; i<mdata_num; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            free(table[i][j]);
        }
        free(table[i]);
    }
    free(table);
Was it helpful?

Solution

There are two ways, static or dynamic. Which one to use depends on whether you know the number of rows or not.

Static

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

#define ROWS 10
#define COLUMNS 9
#define MAX_STRING_SIZE 256

int main(){

  char table[ROWS][COLUMNS][MAX_STRING_SIZE];

  //EXAMPLE OF USE
  for(i=0; i<ROWS; i++)
    for(j=0; j<COLUMNS; j++)
      strcpy(table[i][j], "hi");

}

Dynamic

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

#define ROWS 10
#define COLUMNS 9
#define MAX_STRING_SIZE 256

int main(){

  char ***table = calloc(ROWS, sizeof(char**));

  int i, j;
  for(i=0; i<ROWS; i++){
    table[i] = calloc(COLUMNS, sizeof(char*));
    for(j=0; j<COLUMNS; j++){
      table[i][j] = calloc(MAX_STRING_SIZE, sizeof(char));
    }
  }

  //FREE THE TABLE
  for(i=0; i<ROWS; i++){
    for(j=0; j<COLUMNS; j++){
            fprintf(stderr, "%s, ", table[i][j]);
            free(table[i][j]);
        }
        fprintf(stderr, "\n");
        free(table[i]);
    }
    free(table);

}

Of course you need to check if the calloc calls return NULL. Allocating the strings with calloc ensures that the strings will be initialized with zeros. This algorithms will create 10 rows, with 9 columns being each column a string of 256 chars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top