2

We saw passing arrays to functions using pointers in my intro. to C class, and I'm trying to learn how to pass multidimensional arrays on my own. I tried writing a function to assign the values of the entries of a matrix onto a local array, but I get a segmentation fault. I was hoping someone could explain why this happens and how to fix it. I'm using the terminal on macOS Sierra. Thanks in advance. My code is below:

#include <stdio.h>
#include <stdlib.h>

void fillMatrix();

int main(void){
    int rows, cols;

    printf("\nEnter the number of columns:\n");
        scanf("%d", &cols);
    printf("\nEnter the number of rows:\n");
        scanf("%d", &rows);

    int matrix[rows][cols];


    fillMatrix(&matrix[rows][cols], rows, cols);

    for (int i = 0; i < rows; ++i){
        for (int j = 0; j < (cols - 1); ++j){
            printf("%d ", matrix[i][j]);
        } printf("%d\n", matrix[i][(cols -1)]);
    }
    return 0;
}

void fillMatrix( int *matrix, int rows, int cols ){
    for (int i = 0; i < rows; ++i){
        for (int j = 0; j < cols; ++j){
            printf("\nPlease enter the A(%d,%d) entry:\n", i, j);
                scanf("%d", &*(matrix + (i*cols) + j));
        }
    }
    return;
}
Alex D
  • 149
  • 6

2 Answers2

4

Given the declaration

int matrix[rows][cols];

This code is wrong:

fillMatrix(&matrix[rows][cols], rows, cols);

The address of &matrix[rows][cols] is past the end of the matrix.

The first element of the matrix is &matrix[0][0], and the last element of the matrix is &matrix[rows-1][cols-1].

Also, this declaration

void fillMatrix();

will cause problems with this defintion:

void fillMatrix( int *matrix, int rows, int cols ){
    ...

They need to match. Right now, because of the void fillMatrix() declaration up top, arguments get passed to the function via default argument promotion, but because the definition has explicit arguments, the function itself expects the arguments to be passed as int * or int. You're probably not having problems with that as the defaults for those arguments are likely the same as those arguments, but function definitions and declarations generally must match exactly.

I haven't examined your code for other issues.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • Thank you this fixed it! I'm guilty of copy-pasting without thinking about it or checking twice. "You can accept an answer in seven minutes" – Alex D Nov 17 '18 at 01:07
  • @AlexD I noticed something else, too. Your function definition and declaration don't match for `fillMatrix()`. – Andrew Henle Nov 17 '18 at 01:12
1

In C when you are declaring an array you need to specify its size at the time of compilation. When you decelerate the array in line

    int matrix[rows][cols];

You actually initialise its size with rubbish values. In case of my compiler it was initialised with size of [0][0]. In order to achieve what you want you need to do one of two things:

  1. Specify explicitly what is the size of the array before compilation
  2. Dynamically allocate space for the array
Mateusz Stompór
  • 461
  • 6
  • 15
  • "you need to specify its size at the time of compilation" -- not true since C99 with the addition of variable length arrays, which are sized at runtime (though these were made optional again with C11, VLAs are still available almost everywhere). Also, `rows` and `cols` don't have garbage values in OP code unless the user input is bad (so OP _should_ validate the input before using it to avoid undefined behavior.) – ad absurdum Nov 17 '18 at 01:25