1

Okay so, I have a twodimensional array of int's, which in create using this:

int **matrix;
matrix = malloc(n * sizeof(int*));
for (int i = 0; i < n ; i++) {
 matrix[i] = malloc(sizeof(int));
}

Now I set every field to 0. The next step is to fill it with values. Therefore I read values from a file I already opened.

while (fscanf(f2, "%s -> %[^;\n]%*c", start, ziel) == 2) {
 intomatrix(start, ziel, &graph);
}

intomatrix just alters the data and puts it in the matrix using

matrix[start][ziel] = 1;

Now when I set manually (not using the while-loop)

matrix[2][0] = 1

and print the matrix. Not only is (2,0) but also (0,4) set to 1. There should be now way in which matrix[0][4] is used.

This also happens with other fields. (2,0) also sets (1,4).

asked May 28, 2015 at 16:32
2
  • matrix[i] = malloc(sizeof(int)); allocates for array of 1 element. Should be matrix[i] = malloc(ELEMENTS*sizeof(int)); Commented May 28, 2015 at 16:35
  • wow... @WeatherVane that was the problem. Now its solved. Thx :) Commented May 28, 2015 at 16:37

1 Answer 1

2

Should this:

matrix[i] = malloc(sizeof(int));

be an array of integers vs. just one? If not you have multiple rows but just one column.

answered May 28, 2015 at 16:36
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.