Hello,
I have run into a problem. I am trying to pass a two dimensional pointer and write data to it. The only thing is it always crashes if I write more than one array index. For example:
It may be something im doing wrong, or not allocating enough space. I don't know, thats why I'm here I've spent 30 minutes trying to figure it out and still have no clue.Code:#include <stdio.h> #include <stdlib.h> void passtest(char ***string) { // Two strings *string = (char **)malloc(sizeof *string * 2); if (*string) { // Works great (*string)[0] = (char *)malloc(sizeof *string[0] * 6); strcpy(*string[0], "Hello"); // Crashes... (*string)[1] = (char *)malloc(sizeof *string[1] * 3); strcpy(*string[1], "Hi"); } } int main() { int i; char **test = NULL; passtest(&test); for (i = 0; i < 2; i++) free(test[i]); free(test); return 0; }
Thank you for your time,
- Stack Overflow