This is my code:
#include <stdio.h>
#include <stdlib.h>
struct pixel
{
int r, g, b;
};
int main ()
{
int width, height, i, j, opCode;
struct pixel **img;
printf("opCode = ");
scanf("%d", &opCode);
printf("Numarul de coloane = ");
scanf("%d", &width);
printf("Numarul de linii = ");
scanf("%d", &height);
img = malloc(height * width * sizeof(int *));
for ( i = 0; i < height; i++)
{
for ( j = 0; j < width; j++)
{
img[i][j].r = scanf("%d ", &img[i][j].r);
img[i][j].g = scanf("%d ", &img[i][j].g);
img[i][j].b = scanf("%d ", &img[i][j].b);
}
}
free(*img);
return 0;
}
When I read opCode
, height
and width
it works just fine. But after I try to read the first element of the matrix img
, it gives me a segmentation fault (core dumped). I tried to use Valgrind to figure out what's wrong, but I can't figure out the problem.