1. ## Segmentation Fault

I get a segmentation fault when I run this program. I attempted debugging, and I think the problem is in my pointer B, but I don't know why. The fault comes in the function "loesen" as soon as B is used

Code:
```#include <stdio.h>
int k=4;
int eingabe(int i, int j, int n, int k, double **A, double *B){

A = malloc(k * sizeof(**A));
for(i=0; i<k; i++)
A[i]=malloc(k * sizeof(**A));
printf("Geben Sie ein Oberematrix Reihweise ein\n");

for(i=0; i<k; i++){
for(j=i; j<k; j++){
scanf("%lf", &A[i][j]);

}
}
B = malloc(k * sizeof(*B));
printf("Geben Sie Loesung Vektor ein\n");
for(i=0; i<k; i++)
scanf("%lf", &B[i]);
return 0;

}
int ausgabe(int i, int k, double *X){
for(i=0; i<k; i++)
printf("%lf", X[i]);
return 0;
}
int loesen(int i, int j, int k, int n, double det, double *X, double *B, double **A, double summe){
X = malloc(k * sizeof(*X));
summe = 0;
n = k-1;
X[n] = B[n] / A[n][n];
for(i=0; i<k; i++){
for(j=i+1; j<n; j++)
summe += A[i][j] * X[j];
X[i] = (1/A[i][i])* (B[i]-summe);
}
for(i=0; i<k; i++)
det *= A[i][i];
if(det==0)
printf("Matrix ist Singulaer\n");

return 0;
}
int main(){
int i, j, n;
double *X, *B, **A, summe, det;
eingabe( i, j, n, k, A, B);
loesen( i, j, k, n, det, X, B, A, summe);
ausgabe( i, k, X);
getch();
return 0;

}```

2. Both A and B will remain unchanged after the return from eingabe since they are local variables inside eingabe.

3. But I don't try to change A or B. I just want to access their storage locations to call up the values so that I can do calculations. I should still be able to access them, right?

4. Damn, it's been too many times I encountered this.
And think carefully.

Code:
```
A = malloc(k * sizeof(**A));
//  you mean ?
A = malloc(k * sizeof(*A) );```

5. ok, so I thought about it carefully and I would like to see if what I think so far is right. The problem is that my functions are only editing a copy of the pointer, so when I try to do use the values in the memory locations, I never actually allocated a value to them.

In the link, the suggested solutions are as follows: either
1) split my "eingabe" function into 2 functions and return the location of the pointers(&A and &B)
or
2) make a pointer point to the memory locations of my vectors/matrix

Am I close?

To the code, I have used what I typed previously and it worked, so I don't konw why it wouldn't this time.

6. apparently trying to return A and B doesn't work like I thought it would. I just don't understand the whole pointer thing as well as I should. I don't want to waste your time with trying to explain it to me step by step.

7. If you want matrix, it's better to use struct.
Code:
```typedef struct {
int row,col;
double **value;
} matrix;```
If you still want prefer original approach,
Code:
`int eingabe(int i, int j, int n, int k, double ***A, double **B){`

8. You're not returning anything at all.
Just because you have a * in there doesn't make it magically return things.

Code:
```void foo ( int a ) {
a = 1;
}
int main ( ) {
int b = 0;
foo( b );
// what is b now?
}```
To make it work, you need a pointer to the variable, from the callers perspective.
Code:
```void foo ( int *a ) {
*a = 1;
}
int main ( ) {
int b = 0;
foo( &b );
// what is b now?
}```

Now in your case, this would become
Code:
```void foo ( double ***a ) {
// well you get the idea by now ;)
}
int main ( ) {
double **b = 0;
foo( &b );
}```

9. Don't pass in variables that you don't read from before they are written to.
Take the simplest function here:
Code:
```int ausgabe(int i, int k, double *X){
for(i=0; i<k; i++)
printf("%lf", X[i]);
return 0;
}```
The first things that happen with each variable are:
'i' is set to 0
'k' is read to compare it with 'i'
'X' is read and used to access an array
This indicates that 'i' is not meant to be a parameter to this function.

Also, don't return something if you don't ever intend to do anything with that value (except in main).
Lets correct these things:
Code:
```void ausgabe(int k, double *X) {
int i;
for (i=0; i<k; i++)
printf("%lf", X[i]);
}```
Your other two functions have the same problems. Try fixing those the same way.