1

I've tried to create a matrix in C and have some input value, but I don't know why it throws me a "segmentation error". This is my code:

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

int main() {
    int i;
    int j;
    int **a;
    a = malloc(5 * sizeof(int));
    for (i = 0; i < 5; i++) {
      a[i] = malloc(4 * sizeof(int));
    }
    for (i = 0; i < 5; i++) {
      for (j = 0; j < 4; j++) {
        scanf("%d", (a[i][j]));
      }
    }
    return 0;
}
2
  • 1
    It should be scanf("%d", &(a[i][j])); Commented Dec 5, 2016 at 11:19
  • 1
    If you compile with gcc, it warns you you're using scanf wrong, resolving gcc warning often solve a lot of problem. Commented Dec 5, 2016 at 11:22

2 Answers 2

2

Given the answer by @Bathsheba, this is what your code should look like:

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

int main (void)
{
  int (*a)[4] = malloc( sizeof(int[5][4]) );

  for(int i=0;i<5;i++)
  {
    for(int j=0;j<4;j++)
    {
      scanf("%d", &a[i][j]);
    }
  }

  free(a);
  return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Upvoted. This is the "ace" answer, but I don't have enough confidence to use it, especially when you have to consider decay to pointers.
@Bathsheba Array pointers don't decay. When passing this array pointer to a function, you would simply declare the function as void func (size_t x, size_t y, int array[x][y]). Where int array[x][y] will get adjusted by the compiler ("decay") into a pointer to the first element: int (*array)[y]. Which is exactly what we have in the caller.
That's helpful. In fact much of this is above my pay grade. Do feel free to take facets from my answer and incorporate it into this one.
@Bathsheba Nah, your answer is fine, I just couldn't stand by watching you die inside :)
0
  1. Your allocation for a should be

a=malloc(5*sizeof(int*));

Note well the pointer type in the sizof. On some systems (Windows desktops in the early 2000s) an int just happened to be the same size as an int*, but you must not assume that.

  1. scanf takes a pointer as the parameter: the clearest way to write this is scanf("%d", &a[i][j]);

  2. Don't forget to free the memory once you're all done.

Finally, I die a little inside every time I see a matrix modelled like this. Granted, it allows you to use the [][] notation, but normally it's better to model it as a contiguous memory block and use the idiom i * columns + j to access the element at (i, j).

3 Comments

thanks to help, can you do me a example of use the idiom i * columns+j?
i * columns + j isn't necessary since 1999. One can use a pointer to a VLA and thereby get the convenient [][] syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.