Thread: 2d array as function parameter problem, help appreciated.

  1. #1
    Registered User
    Join Date
    Dec 2009

    2d array as function parameter problem, help appreciated.

    I have been tinkering with this for a few hours and even with any examples can not find a reason for the Seg fault I'm getting, The code is all the way at the bottom of the post.

    I am still new to C after a long python stint. I am trying to implement a jacobian algorithm to find the solution to a system of equations.

    the header of the Jacobi dunction is commented right now to only take the array, and the length(it is assumed to be square)

    the program dies on a segfault, running gdb gave me that it dies right at

    aInv[xCounter][xCounter] = 1.0/A[xCounter][xCounter];
    when i started with this program I hardcoded A right into the function and it went through no problems so this must be with the way im taking in and handling A.

    Im sure the solution isnt terribly difficult but i've exhausted any similar examples I could find and hopefully someone here can offer guidance.

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    int Jacobi(double **A/*,double *b*/,int n/*,double *xo,double tol,int MaxIt*/){
        USAGE:its = Jacobi(A,b,n,xo,tol,MaxIt);
        INPUTS:A     coefficient matrix for linear system
        b       right-hand side vector
        n       number of equations
        xo      vector containing initial guess
        tol     error tolerance-between successive approximations
        MaxIt   maximum number of iterations
        its     No.of iterations performed(return-1 if max exceeded)
        xo      approximate solution
        int iterCount = 0;
        int i = 0;
        int j = 0;
        double tempDouble = 0;
        /*First we find A^-1 */
        double aInv[n][n];
        int xCounter = 0;
        int yCounter = 0;
        double lower[n][n];
        double upper[n][n];
        while(yCounter < n){
            xCounter = 0;
            while(xCounter < n){
                if(yCounter == xCounter){
                    printf("%f - a number",&A[xCounter][xCounter]);
                    aInv[xCounter][xCounter] = 1.0/A[xCounter][xCounter];
                    lower[xCounter][xCounter] = 0;
                    upper[xCounter][xCounter] = 0;
                    aInv[xCounter][yCounter] = 0;
                    if(xCounter < yCounter){
                        lower[xCounter][yCounter] = A[xCounter][yCounter];
                        upper[xCounter][yCounter] = 0;
                        upper[xCounter][yCounter] = A[xCounter][yCounter];
                        lower[xCounter][yCounter] = 0;
        xCounter = 0;
       /* while (counter < MaxIt){
    int main(int argc, char *argv[]){
        printf("GOT HERE");
        double a[2][2] = {2,3,5,7};
    /*Prints a 2d square array of double, for debugging purposes*/
    void PrintArray(double** Arr, int size, char* name){
    int counterX = 0;
    int counterY = 0;
    printf("Printing %s Array...",name);
        while(counterY < size){
            while(counterX < size){
    int breakpt(){
    return 1;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you're starting with double a[2][2] as your input variable, then the prototype for the function can(*) also be

    int Jacobi ( double a[2][2], int MaxIt );

    Likewise, with the function definition, and you just use normal array subscripting inside the function as if you were writing the same code in main itself.

    And the call would simply be
    Jacobi( a, 2 );

    The declaration can also be expressed like this as well, but it's more typing.
    int Jacobi ( double a[][2], int MaxIt );
    int Jacobi ( double (*a)[2], int MaxIt );
    The second form shows the true type of the pointer, which is a "pointer to an array of two doubles".

    This is wrong for a true 2D array...
    int Jacobi ( double **a, int MaxIt );
    Where a is now a "pointer to a pointer to a double".
    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.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Quote Originally Posted by Salem View Post
    If you're starting with double a[2][2] as your input variable, then the prototype for the function can(*) also be

    int Jacobi ( double a[2][2], int MaxIt );
    Right I agree with you and thats where the majority of examples online go to. I think my code was a little misleading because of its infantile state. eventually the function should be able to take in an array of any size that will be dictated by a user's input.

    in my case the array in the parameter must be able to be of any size. I'm still stuck on that part.

  4. #4
    Registered User
    Join Date
    Dec 2009
    So, After some tinkering I figured out that if i do my function decleration like this:

    int Jacobi(int n,double A[n][n]){

    and declare the size in the array, it seems to not seg fault there. my question becomes, is there a different better way to do this?
    Last edited by somekid413; 01-17-2010 at 03:10 PM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    int Jacobi(int n,double A[n][n])
    This is OK according to the more recent C standard, but not all compilers will support it. If you don't care about portability, this should be acceptable. If you do care, you should ditch arrays and switch to pointers from the outset.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread