I'm trying to obtain a char * matrix in C language, but runtime errors occurred to me. The following code shows how I've tried to do this. Can anyone tell me where I'm wrong and why? I'm new in C programming, but I came from Java's and PHP's worlds. Thanks in advance for your interesting and help
int rows = 10;
int cols = 3;
//I create rows
char *** result = calloc(rows, sizeof(char **));
//I create cols
for (int i = 0; i < cols; i++)
{
result[i] = calloc(cols, sizeof(char *));
}
//Load values into the matrix
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = (char *)malloc(100 * sizeof(char));
if (NULL != result[i][j])
{
strcpy(result[i][j], "hello");
}
}
printf("\n");
}
//Print the matrix
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%s\t", result[i][j]);
}
printf("\n");
}
Ps: I'm using xCode with C99
The runtime error occurs here:
result[i][j] = (char *)malloc(100 * sizeof(char));
xCode returs me EXC_BAD_ACCESS