S
sunilkjin
Hi ,
I am trying to alllocate a 2D array of strings and returning it
to a functiion, I get a memory access error. What is wrong with the
program?
int main3DArray( char *** x);
int main()
{
int i,j;
char **p;
main3DArray(&p);
for(i=0,j=0; i< 5;j++, i++)
// cout << p[j] << endl;
return 0;
}
int main3DArray( char *** x)
{
const int l=10;
const int m=10;
const int n=10;
int j,i;
x = (char ***)malloc ( n * sizeof (char ** ));
for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));
for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d array of
"TEST"
}
return 0;
}
Thanks
Sledge
I am trying to alllocate a 2D array of strings and returning it
to a functiion, I get a memory access error. What is wrong with the
program?
int main3DArray( char *** x);
int main()
{
int i,j;
char **p;
main3DArray(&p);
for(i=0,j=0; i< 5;j++, i++)
// cout << p[j] << endl;
return 0;
}
int main3DArray( char *** x)
{
const int l=10;
const int m=10;
const int n=10;
int j,i;
x = (char ***)malloc ( n * sizeof (char ** ));
for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));
for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d array of
"TEST"
}
return 0;
}
Thanks
Sledge