I have an array of String (char **
) which is initialised as NULL
. After passing its address when I try to access its elements it gives segmentation fault.
//following code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void parse(char ***test, char *str)
{
int i;
*test = (char**)malloc(sizeof(char*) * 3);
for(i=0; i<3; i++)
{
*test[i] = (char*) malloc(sizeof(char)*(strlen(str)+1));
strcpy(*test[i], str);
}
}
int main(void)
{
int i;
char *str = "Hello world";
char **test = NULL;
parse(&test, str);
for(i=0; i<3; i++)
printf("%s\n", test[i]);
return 0;
}
On using debugger inside the function parse all elements have correct values and properly initialised and allocated but from main function only 0 indexed row gives the correct value rest are segment fault.