Hello guys!!! It's been a long time since I have been here but I ran into a problem today. I am trying to populate a list of strings (I am not using the string library here, pointers. I know strings are easier but some of the libraries I am using are not compatible!!!) So here is what I am trying to accomplish:
Dynamic list of strings:
Hello
World
Test
String
List
Here is how I am trying to load my dynamic list:
Code:
char **stringList;
int count;
int LoadList () {
stringList = (char **) malloc (1);
for (int i = 0; i < 10; i++) {
char *string;
scanf ("%s", string); // i know scanf is bad, this is an example
// also, i know the string would be
// allocated first, but just imagine
// that scanf takes care of that for this
// example
stringList[i] = (char *) malloc (strlen (string) + 1);
strcpy (stringList[i], string);
temp = (char **) realloc (stringList, i+2);
if (temp == NULL)
break;
stringList = temp;
}
}
This is a crude example of what I am doing. Scanf of course does not allocate space but in my example imagine that it does. If this is not how I am supposed to accomplish this, can anyone give me an idea how to fix it? Thanks!!!