0

I have the following program which uses a pointer to pointer to print the different elements of a 2D array U.

#include <stdio.h>

int myfunction(double **U) {
    printf("%f\n", U[0][0]); //correctly prints U[0][0]
    printf("%f\n", U[0][1]); //correctly prints U[0][1]
    printf("%f\n", U[1][0]); //"Segmentation fault (core dumped)"
    return 0;
}

int main(void){    
    double U[3][4] = {{10, 1, 0, 0}, {2, 6, 0, 0}, {4, 3, 5, 0}};
    double **pU = U;

    myfunction(&pU);
    return 0;
}

Running the code gives the following output:

10.00000
1.00000
Segmentation fault (core dumped)

Why is it that **U seemingly can only access the first row of the matrix? What should I do differently so that printf("%f\n", U[1][0]); prints the correct value?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
A. A. Pedersen
  • 161
  • 1
  • 1
  • 8

0 Answers0