Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Problem with double pointers/matrix GCC
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 05-26-2011, 11:51 PM #1 Zaizeku LQ Newbie   Registered: Sep 2008 Posts: 2 Rep: 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 #include int **NxN_Matrix(long rows, long columns); int main() { int **test; int i,j; test = NxN_Matrix(10,2); for(i = 0; i < 2; i++) { for(j = 0; j < 10; j++) { test[i][j] = i; } } for(i = 0; i < 2; i++) { for(j = 0; j < 10; j++) { printf("[%d]\n",test[i][j]); } printf("\n"); } return 0; } int **NxN_Matrix(long rows, long columns) { long i; int **matrix; matrix = (int**)calloc(rows,sizeof(int*)); for(i = 0; i < rows; i++) { matrix[i] = (int*)calloc(columns, sizeof(int)); } return (matrix); }``` Last edited by Zaizeku; 05-26-2011 at 11:54 PM.
 05-27-2011, 03:01 AM #2 markush Senior Member   Registered: Apr 2007 Location: Germany Distribution: Slackware Posts: 3,979 Rep: Hello Zaizeku, on my system (Slackware64-13.37) I get with your code Code: ```markus@samsung:~/Programmierung/c\$ gcc -o matrix matrix.c markus@samsung:~/Programmierung/c\$ ./matrix [0] [0] [0] [0] [0] [0] [0] [0] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] markus@samsung:~/Programmierung/c\$``` which seems to be the same as your output from Visual Studio. Markus
 05-27-2011, 04:19 AM #3 Ramurd Member   Registered: Mar 2009 Location: Rotterdam, the Netherlands Distribution: Slackwarelinux Posts: 675 Rep: 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 #include int **NxN_Matrix(long rows, long columns); int main() { int **test; int i,j; test = NxN_Matrix(10,2); for(i = 0; i < 2; i++) { for(j = 0; j < 10; j++) { test[i][j] = i; printf("Assigned: test[%d][%d](%p)=%d; Value=%d\n",i,j,&test[i][j],i,test[i][j]); } printf("\n"); } for(i = 0; i < 2; i++) { for(j = 0; j < 10; j++) { printf("%d x %d = [%d]\n",i,j,test[i][j]); } printf("\n"); } return 0; } int **NxN_Matrix(long rows, long columns) { long i; int **matrix; matrix = (int**)calloc(rows,sizeof(int*)); printf("matrix=%p\n",matrix); for(i = 0; i < rows; i++) { matrix[i] = (int*)calloc(columns, sizeof(int)); printf("%p[%d]=%p\n",matrix,i,matrix[i]); } return (matrix); }```
 05-27-2011, 04:55 AM #4 markush Senior Member   Registered: Apr 2007 Location: Germany Distribution: Slackware Posts: 3,979 Rep: Hi, I've changed your code to something more general. Code: ```#include #include int **NxN_Matrix(int rows, int columns); int main() { int **test; int i,j; int r=12, c=3; test = NxN_Matrix(r,c); for(i = 0; i < c; i++) { for(j = 0; j < r; j++) { test[i][j] = i; } } for(i = 0; i < c; i++) { for(j = 0; j < r; j++) { printf("[%d]\n",test[i][j]); } printf("\n"); } return 0; } int **NxN_Matrix(int rows, int columns) { int i; int **matrix; matrix = (int**)calloc(columns, sizeof(int)); for(i = 0; i < columns; i++) { matrix[i] = (int*)calloc(rows, sizeof(int)); } return (matrix); }``` I've swaped rows and columns in the allocation part, now it works for me. It turns out, that your code works with rows up to a size of 8, but with longer rows you'll get wrong numbers on any position higher than 7. Markus
 05-27-2011, 05:49 AM #5 dwhitney67 Senior Member   Registered: Jun 2006 Location: Maryland Distribution: Kubuntu, Fedora, RHEL Posts: 1,533 Rep: 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]` Perhaps a diagram may enlighten the situation: Code: ``` Row Columns test ---> [0] ---> [0][1] [1] ---> [0][1] [2] ---> [0][1] . . . [9] ---> [0][1]``` Thus code-wise, the 'test' matrix is setup as such: Code: ```for (int r = 0; r < rows; ++r) { for (int c = 0; c < columns; ++c) { test[r][c] = c; } }``` Here's the complete code (using the C99 std): Code: ```#include #include #include int** NxN_Matrix(const int rows, const int columns) { int** matrix = calloc(rows, sizeof(int*)); assert(matrix != NULL); for (int r = 0; r < rows; ++r) { matrix[r] = calloc(columns, sizeof(int)); assert(matrix[r] != NULL); } return matrix; } void NxN_Matrix_destroy(int** matrix, const int rows) { for (int r = 0; r < rows; ++r) { free(matrix[r]); } free(matrix); } int main() { const int rows = 10; const int cols = 2; int** test = NxN_Matrix(rows, cols); for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { test[r][c] = c; printf("[%d]\t", test[r][c]); } printf("\n"); } NxN_Matrix_destroy(test, rows); return 0; }``` To build: Code: `gcc -Wall -pedantic -std=c99 matrix.c -o matrix` Last edited by dwhitney67; 05-27-2011 at 05:53 AM. 1 members found this post helpful.
 05-27-2011, 11:28 AM #6 Zaizeku LQ Newbie   Registered: Sep 2008 Posts: 2 Original Poster Rep: 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. Last edited by Zaizeku; 05-27-2011 at 11:40 AM.
 05-27-2011, 11:34 AM #7 MTK358 LQ 5k Club   Registered: Sep 2009 Posts: 6,443 Blog Entries: 3 Rep: Mark the thread as solved.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post kponenation Programming 23 03-16-2011 11:43 AM njanani Programming 5 06-29-2007 02:59 AM _john_i_ Programming 2 01-12-2007 01:11 PM kvtournh Programming 9 03-20-2006 04:38 AM alaios Programming 3 09-15-2005 11:34 AM

All times are GMT -5. The time now is 11:57 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -