Hi all,
I am having some trouble with using malloc() and a (char **). Here is a look at my code:
I'm trying to stay away from using an array of any sort like a char *[], I was just hoping there was a way to allocate memory to a (char **). This is also C, so yes, I would have used new and delete in a split second if it were C++.Code:#include <stdio.h> #include <stdlib.h> #include <string.h> struct Words { char **wordList; }; int main() { int l; int listCount = 4; char words[5][25] = {"Season", "Fan", "Car", "Fruit"}; Words *filter; filter = (Words*) malloc ( sizeof(Words) ); // Write bad words to char array for (l = 0; l < listCount; l++) { filter->wordList[l] = (char *) malloc (strlen(words[l]) + 1); strcpy( filter->wordList[l], words[l] ); } printf("%s\n", filter->wordList[0]); for (l = 0; l < listCount; l++) { free((char *)filter->wordList[l]); } free((Words*)filter); return 0; }
Thank you for your time,
- Stack Overflow