Thread: segmentation fault when assigning a value to a 2d array

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    39

    segmentation fault when assigning a value to a 2d array

    I wrote this code that asks the user to input the number of triangles and triplets.

    however when i print out the array elements I get a seg fault.

    During the first iteration that the user is requested to enter the number of triplets everything works fine, after that it only prints out garbage.

    Could anyone tell me where I went wrong?

    #include < stdio.h >
    #include < stdlib.h >
    #define request "Please enter the number of triangles to check: "
    #define triplet1 "Please enter the first number of the triplet: "
    #define triplet2 "Please enter the second number of the triplet: "
    #define triplet3 "Please enter the third number of the triplet: "
    #define Error "[ERR] Invalid number of triangles.\n "
    #define Error2 "[ERR] Invalid number for the triplet.\n"
    #define UCHAR_MAX 20

    Code:
    int main() {
    
      int triangles = 0;
      char * end;
    
      for (;;) {
        printf("%s", request);
        char buffer[30];
        fgets(buffer, sizeof(buffer), stdin);
        triangles = strtol(buffer, & end, 10);
        if ( * end != '\n' || triangles < 1 || triangles > UCHAR_MAX) {
          printf("%s", Error);
    
          continue;
    
        }
        break;
    
      }
    
      float arr[triangles][3];
      int i = 0;
      int j = 0;
      while (i < triangles) {
        float tmp = 0.0;
        char buffer[30];
        while (1) {
          printf("%s\n", triplet1);
          fgets(buffer, sizeof(buffer), stdin);
          tmp = strtol(buffer, & end, 10);
          if ( * end != '\n' || tmp < 1) {
            printf("%s", Error2);
            continue;
          }
          arr[i][j] = tmp;
          j++;
          break;
    
        }
    
        tmp = 0.0;
        while (1) {
          printf("%s\n", triplet2);
          fgets(buffer, sizeof(buffer), stdin);
          tmp = strtol(buffer, & end, 10);
          if ( * end != '\n' || tmp < 1) {
            printf("%s", Error2);
            continue;
          }
          arr[i][j] = tmp;
          j++;
    
          break;
        }
        tmp = 0.0;
        while (1) {
          printf("%s\n", triplet3);
          fgets(buffer, sizeof(buffer), stdin);
          tmp = strtol(buffer, & end, 10);
          if ( * end != '\n' || tmp < 1) {
            printf("%s", Error2);
            continue;
          }
          arr[i][j] = tmp;
          j++;
          break;
    
        }
        i++;
      }
    
      for (i = 0; i < triangles; i++) {
    
        for (j = 0; j < 3; j++) {
          printf("a[%d][%d] = %f\n", i, j, arr[i][j]);
        }
      }
    
      return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    After you read your final number in the triplet, you need some mechanism to reset j to zero as your loop re-starts; so perhaps that final case should have j=0 instead of j++. Although: if you hadn't set up your strings as hard-coded prompts, you could wrap the whole thing in a nested for-loop (outside on i from 0 to < triangles, inside on j from 0 to < 3) so that you wouldn't have to duplicate that code three times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-02-2012, 01:36 PM
  2. 2d array segmentation fault
    By Max2011 in forum C Programming
    Replies: 1
    Last Post: 05-21-2011, 10:30 AM
  3. Segmentation fault while using 2D array
    By Damon Dike in forum C Programming
    Replies: 7
    Last Post: 04-03-2010, 08:23 AM
  4. Replies: 8
    Last Post: 12-08-2009, 02:47 AM
  5. Segmentation fault with array?
    By whiphub in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 01:51 PM

Tags for this Thread