SHARE
TWEET

Untitled

a guest Jul 19th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. void getRestaurants(char ***restaurantsArray) {
  6.     FILE *restaurantsFile = fopen("./restaurants.txt", "r");
  7.     char *restaurant = (char *)malloc(50 * sizeof(char));
  8.     char *restaurantCopy = restaurant;
  9.  
  10.     //fopen will return null if it is unable to read the file
  11.     if (restaurantsFile == NULL) {
  12.     free(restaurant);
  13.     return;
  14.     }
  15.  
  16.     int index = 0;
  17.     while (fgets(restaurantCopy, 50, restaurantsFile)) {
  18.         // segfault occurs the third time the following line is executed
  19.         *restaurantsArray[index] = (char*)malloc(50 * sizeof(char));
  20.         strcpy(*restaurantsArray[index], restaurantCopy);
  21.         printf("%s", restaurantCopy);
  22.         printf("%s", *restaurantsArray[index]);
  23.         index++;
  24.     }
  25.  
  26.     fclose(restaurantsFile);
  27.     free(restaurant);
  28. }
  29.  
  30. void main() {
  31.     char **restaurantsArray = (char **)malloc(100 * sizeof(char *));
  32.     char **restaurantsArrayCopy = restaurantsArray;
  33.     getRestaurants(&restaurantsArrayCopy);
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top