Here is the code:
#include <stdio.h>
#include <stdlib.h>
#define numOfStrings 10
#define sizeOfString 30
void crashControl();
int main()
{
char **strArray = (char **)malloc(numOfStrings*sizeof(char *));
crashControl(strArray);
for (int i = 0; i < numOfStrings; i++)
{
strArray[i] = (char *)malloc(sizeOfString*sizeof(char));
crashControl(strArray[i]);
}
return 0;
}
void crashControl(char *A)
{
if (!A)
{
printf("Not enough space.\n");
exit(1);
}
}
It seems work correctly. When I increase numOfStrings
too much crashControl(strArray)
works correctly. Likewise when I increase sizeofString
too much crashControl(strArray[i])
works correctly as well. But I wonder, am I doing right or wrong? Is there a risk or a bug for this code? The parameter of crashControl()
function has one dimensional array, am I free to use this function for any N-dimensional array?