Problem with double pointers/matrix GCC
Hi guys I'm having a problem that I do not know nor have I found a way to solve. Let's say I have a 10x2 matrix, I create said matrix and fill it one column with zeros and the other with ones, now when I print the matrix I get this:
column 1: 0,0,0,0,0,0,0,0,1,1 column 2: 1,1,1,1,1,1,1,1,1,1 The first two rows of the second column are being replaced into the last two rows of the first column, now I even checked in visual studio and it works fine there. A friend tried my code and he gets it even worse: column 1: 0,0,0,0,1,1,1,1,1,1 column 2: 1,1,1,1,1,1,1,1,1,1 As far as I've seen it must be a problem with GCC, unfortunately I need to have this up and running in GCC no matter what. Here I leave the test code. So if you guys have a solution to this I'm all ears.Thanks in Advance. Code:
#include<stdio.h> |
Hello Zaizeku,
on my system (Slackware64-13.37) I get with your code Code:
markus@samsung:~/Programmierung/c$ gcc -o matrix matrix.c Markus |
To debug your code, I changed it a little bit:
This shows that the address assigned to test[0][8] is the same address as test[1][0], so it gets overwritten; I need more coffee to see where the calculation goes wrong. Code:
#include<stdio.h> |
Hi,
I've changed your code to something more general. Code:
#include<stdio.h> Markus |
Based on the code in the OP, the matrix should be accessed using row, then column. After all, that is how it was setup. For example:
Code:
test[row][column] Code:
Row Columns Code:
for (int r = 0; r < rows; ++r) Code:
#include <stdlib.h> Code:
gcc -Wall -pedantic -std=c99 matrix.c -o matrix |
Thanks everyone I guess I was too sleepy to notice that I swapped the columns with the rows by mistake, thank you all who responded now it works.
|
Mark the thread as solved.
|
All times are GMT -5. The time now is 09:58 AM. |