Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- void getRestaurants(char ***restaurantsArray) {
- FILE *restaurantsFile = fopen("./restaurants.txt", "r");
- char *restaurant = (char *)malloc(50 * sizeof(char));
- char *restaurantCopy = restaurant;
- //fopen will return null if it is unable to read the file
- if (restaurantsFile == NULL) {
- free(restaurant);
- return;
- }
- int index = 0;
- while (fgets(restaurantCopy, 50, restaurantsFile)) {
- // segfault occurs the third time the following line is executed
- *restaurantsArray[index] = (char*)malloc(50 * sizeof(char));
- strcpy(*restaurantsArray[index], restaurantCopy);
- printf("%s", restaurantCopy);
- printf("%s", *restaurantsArray[index]);
- index++;
- }
- fclose(restaurantsFile);
- free(restaurant);
- }
- void main() {
- char **restaurantsArray = (char **)malloc(100 * sizeof(char *));
- char **restaurantsArrayCopy = restaurantsArray;
- getRestaurants(&restaurantsArrayCopy);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement