I have searched and searched for a solution to this, can find plenty of answers about multidimensional arrays of int, float, double, but no char*. I think I have a grasp of the principles of pointers, know the difference between char, char* and char[] etc but a pointer to a 2D array of char pointers has got the better of me. I'm trying to parse a csv file and fill my 2D array with strings (char*). Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COLS 10
#define ROWS 1000
int main (void) {
char***myarray;
FILE *fp;
char charTemp[100];
char *varTemp = NULL;
char *strTemp = NULL;
int i, j;
// allocate memory to hold array ROWS
if (( myarray = (char***) malloc(ROWS * sizeof(char**))) == NULL )
return -1;
// then allocate memory to hold array COLS
for (i = 0; i < ROWS; i++)
{
if (( myarray[i] = (char**) malloc(COLS * sizeof(char**))) == NULL )
return -2;
}
// read file
if ((fp = fopen ("myfile.csv", "r")) == NULL)
return -3;
// parse and fill 'myarray'
i = 0;
j = 0;
while (!feof(fp) && fgets(charTemp, sizeof charTemp, fp)) {
strTemp = strtok(charTemp, ",");
while (strTemp != NULL) {
sscanf(strTemp, "%s", &varTemp);
myarray[i][j] = varTemp;
printf("%s ", myarray[i][j]);
j++;
if (j > COLS - 1)
j = 0;
strTemp = strtok( NULL, "," );
}
printf("\n");
i++;
}
return 0;
}
myfile.csv looks something like this:
ABCD,1,0.2,0.5,0,A123,ZZ,1,120,1
BCDE,1.038,0,0.525,0,B321,YY,1.25,100,0.7
CDEF,1,0.2,0.5,0,C3P0,XX,1,120,1
DEFG,,,,,,,,,
EFGH,1,0.3,0.8,0,R2D2,WW,1.25,120,1
FGHI,,,,,,,,,
etc.....
I know some are ints and floats etc but I want them all to go in as char* then I can atoi or whatever when I need to use them.
The printf is just to view what I have loaded to test. If I use token %.*s it displays, if I use %s it segfaults at the printf line. I assume this means I am missing null pointers at the end of my strings??
Debugging suggests varTemp is using memory out of bounds. Additionally, when using printf with %.*s
on rows with no data after first key, it prints COL 0 in COL 1 position as well where there should be a NULL
pointer. i.e.:
ABCD 1 0.2 0.5 0 A123 ZZ 1 120 1
BCDE 1.038 0 0.525 0 B321 YY 1.25 100 0.7
CDEF 1 0.2 0.5 0 C3P0 XX 1 120 1
DEFG DEFG
EFGH 1 0.3 0.8 0 R2D2 WW 1.25 120 1
FGHI FGHI
etc.....
I'm confused, any ideas?!