B
Bill Carson
I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :
int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;
sAtt = (char ***)malloc(nLines * sizeof(char *));
for(m=0; m < nLines; m++)
{
sAtt[m] = (char **)malloc(Cols.sTcolumn * sizeof(char *));
for(n=0; n < Cols.sTcolumn; n++)
sAtt[m][n] = (char *)malloc(20 * sizeof(char));
};
The reason for building this array is to feed it to sscanf() to parse
lines of text into words :
sscanf(lBuffer, formatdbf, &sAtt[m][0], &sAtt[m][1],
&sAtt[m][2], &sAtt[m][3], &sAtt[m][4]);
The above memory allocation may be wrong, because on output
it prints gibberish. I am not sure if there is a problem with my
memory allocation, or whether I need to add terminating null
characters to my sscanf() output strings. If I feed sscanf()
a predefined 3D array that fits my data, such as:
sAtt[1140][10][20];
then everything works fine.
This is my first foray into dynamic memory allocation and
multiple indirection, so if anyone can see my errors, please
let me know.
Thanks,
Bill
with the following (incomplete, for reference only) :
int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;
sAtt = (char ***)malloc(nLines * sizeof(char *));
for(m=0; m < nLines; m++)
{
sAtt[m] = (char **)malloc(Cols.sTcolumn * sizeof(char *));
for(n=0; n < Cols.sTcolumn; n++)
sAtt[m][n] = (char *)malloc(20 * sizeof(char));
};
The reason for building this array is to feed it to sscanf() to parse
lines of text into words :
sscanf(lBuffer, formatdbf, &sAtt[m][0], &sAtt[m][1],
&sAtt[m][2], &sAtt[m][3], &sAtt[m][4]);
The above memory allocation may be wrong, because on output
it prints gibberish. I am not sure if there is a problem with my
memory allocation, or whether I need to add terminating null
characters to my sscanf() output strings. If I feed sscanf()
a predefined 3D array that fits my data, such as:
sAtt[1140][10][20];
then everything works fine.
This is my first foray into dynamic memory allocation and
multiple indirection, so if anyone can see my errors, please
let me know.
Thanks,
Bill