I created a function to read in stations. Input is fine. So what I do is, in main(), pass in the array to the function. Then in the function, realloc the size of the array to the number of stations found reading in the file and then I realloc the number of characters it can hold. The errors I get are:
1) in realloc(): warning: junk pointer, too high to make sense (this is repeated for what looks the number of stations I have)
2) pointer to wrong page
2) Core dumped - seg fault.
Here is my code (not showing stuff that works fine):
In main():
Code:
char **list;
// So want to hold 1 string
list = malloc(1 * sizeof(*list));
// Then want the string to hold 1 char + \0
list[0] = malloc(1*sizeof(char)+1);
read(&*list);
In read():
Code:
// So now realloc to hold "num" strings
*list = realloc(*list, num * sizeof(*list));
// Realloc so that the strings can hold 6 chars now
for(i=0;i<num;++i){
list[i] = realloc(list[i], 6 * sizeof(char));
}
// Now copy the string input
for(i=0;i<num;++i){
strcpy(list[i], input[i]);
}
//print out the new info
for(i=0;i<num;++i){
printf(%s is the new place.\n", list[i]);
}
Any ideas? Interesting is that I can pass a char in, 5, and i can print it inside the function. But after that -- only problems...