Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation fault after scanf on array passed to function
    primarykey
    data
    text
    <p>I'm trying to finish a workshop but I'm getting a segmentation fault after I call inputVector function in the if statement. I have no idea why it's giving me an error. There are no warnings at compile time and the testing on the array passed actually displays what was recorded. I'm running OSX Maverick but I also tested on a linux system to no avail. If someone could shine a light on this I'd really appreciate it. </p> <pre><code>#include &lt;stdio.h&gt; #include "matrix.h" int main() { int row = 0 , col = 0, option = 0; int vector[9], result[9]; int matrix[9][9]; do{ printf("Number of rows (0 to exit) ? "); scanf("%d", &amp;row); if(row != 0) { printf("Number of columns ? "); scanf("%d", &amp;col); inputMatrix(row, col, matrix); printf("Pre (0) or Post (1) Multiply ? "); scanf("%d", &amp;option); if(option == 0){ inputVector(row, vector); printf("TESTING 1"); preMulti(row, col, matrix, vector, result); printf("TESTING 2"); display(col, result); } else if(option == 1){ inputVector(col, vector); postMulti(row, col, matrix, vector, result); display(row, result); } return 0; }// END IF } while( row != 0 );// End While } // End Main </code></pre> <h3>matrix.cpp</h3> <pre><code>#include "matrix.h" #include &lt;stdio.h&gt; void inputMatrix(int row , int col, int matrix[][MAXCOL]) { for ( int i = 0; i &lt; row; i++) { printf("Row %d ? ", i + 1); for ( int k = 0; k &lt; col; k++) scanf("%d", &amp;matrix[i][k]); } } void inputVector(int size, int vector[]) { printf("Vector? "); for(int i = 0; i &lt; size; i++){ scanf("%d" , &amp;vector[i]); printf(" Recorded %d \n", vector[i]); } printf("TESTING 3"); } void preMulti(int row, int col, int matrix[][MAXCOL], int vector[], int result[]) { for(int i = 0; i &lt; col; i++ ) { result[i] = 0; for( int k = 0; k &lt; row; i++) result[i] += vector[k] * matrix[k][i]; } } void postMulti(int row, int col, int matrix[][MAXCOL], int vector[], int result[]) { for( int i = 0; i &lt; row; i++ ) { result[i] = 0; for( int k = 0; k &lt; col; i++) result[i] += vector[k] * matrix[i][k]; } } void display(int size, int vector[]) { printf("Result: "); for ( int i = 0; i &lt; size; i++) printf("%d ", vector[i]); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload