New to C, seg fault

This is a discussion on New to C, seg fault within the C Programming forums, part of the General Programming Boards category; I can't figure out why this would cause a segmentation fault if n < MAXROWS. I'm pretty sure my array ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    New to C, seg fault

    I can't figure out why this would cause a segmentation fault if n < MAXROWS. I'm pretty sure my array bounds are fine as well. Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TOLERANCE 1E-6
    #define MAXROWS 100
    #define MAXCOLS 101
    
    void printMatrix(int rows, int columns, double matrix[][MAXCOLS]);
    
    int main()
    {
            double matrix[MAXROWS][MAXCOLS];
    
            int n;
    
            scanf("%d\n", &n);
    
            if(n < 1){
                    printf("Expected N (number of equations)\n");
                    exit(0);
            }
            int i, j;
    
            for(i = 0; i < n; i++){
                    for(j = 0; j < n+1; j++){
                            int readVal = scanf("%lf", matrix[i][j]);
                            if(readVal < 1){
                                    printf("Element a[%d][%d] is missing\n", i, j);
                                    exit(0);
                            }
                    }
            }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need the address of a variable for scanf to be able to put something there. See the difference between your first and second call to scanf?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Don't you get any compiler warnings about that?

    Code:
    int readVal = scanf("%lf", &matrix[i][j]);

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Just like I thought, a dumb newb mistake. Thanks guys!

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Quote Originally Posted by Subsonics View Post
    Don't you get any compiler warnings about that?

    Code:
    int readVal = scanf("%lf", &matrix[i][j]);
    nope

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Manske View Post
    nope
    Too bad.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    You need to turn up your compiler warnings then.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Does any other compiler apart from GCC perform any kind of validation of printf/scanf format strings and parameters?

    This is yet another good reason to choose GCC IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 09:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21