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

1. 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

Code:
`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.

Code:
```#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
OUTPUT:
its     No.of iterations performed(return-1 if max exceeded)
xo      approximate solution
*/
breakpt();
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;
}
else{
aInv[xCounter][yCounter] = 0;
if(xCounter < yCounter){
lower[xCounter][yCounter] = A[xCounter][yCounter];
upper[xCounter][yCounter] = 0;
}
else{
upper[xCounter][yCounter] = A[xCounter][yCounter];
lower[xCounter][yCounter] = 0;
}
}
xCounter++;

}
yCounter++;
}

xCounter = 0;

PrintArray(aInv,2,"inverse");
PrintArray(lower,2,"lower");
PrintArray(upper,2,"upper");
/* while (counter < MaxIt){

}*/

}

int main(int argc, char *argv[]){

printf("GOT HERE");
double a[2][2] = {2,3,5,7};

Jacobi(&a,2);

}
/*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){
printf("%f",Arr[counterX][counterY]);
counterX++;
}
printf("\n");
counterY++;

}

}
int breakpt(){
return 1;
}```

2. 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".

3. Originally Posted by Salem
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. 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?

5. Code:
`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.