1

I have simple method for prinitng matrix.

void printMatrix(int matrix){ int i; for(i=0;i<sizeof matrix/sizeof matrix[0];i++){ for(j=0; j<sizeof matrix[0]/sizeof matrix[0][0];i++){ printf("%03d",matrix[i][j]); } printf("\n"); } printf("\n"); } 

And in main I have call:

printMatrix(matrix); 

and matrix is defined as:

int matrix[2][3] = {{1, 3, 4}, {2, 0, 1}}; 

Problem is I am getting "Undefined reference to 'printMatrix'" Do you know where could be the problem? Does it require some special include? I don't thinks so, but I don't know, where is the fault.

1
  • In your function void printMatrix(int matrix) you define the argument as an int. It will never, ever have indexes: matrix[i][j] is illegal because matrix is the int argument Commented Mar 26, 2011 at 16:33

4 Answers 4

4

Are you calling the function before it has been defined? If so, try putting the function definition before main.

Sign up to request clarification or add additional context in comments.

1 Comment

undefined reference to... is a linker error - happens after compilation proper, so it's not a problem of missing prototype.
2

You have declared the parameter to printMatrix as an int. Change void printMatrix(int matrix){ to void printMatrix(int matrix[][3]){. Note that you need to define the size of all but the first dimension.

4 Comments

@pmg OP asked "Do you know where could be the problem?" This is a problem, and hence it is an answer. If you notice something the rest of us didn't, please do share rather than being cryptic.
Thanks marcog - now I am getting for the line void printMatrix(int matrix[2][3]){ error - of conflicting types
OP asked "Do you know where could be the problem?" in the context of "Undefined reference to 'printMatrix'" error. So, pmg is correct. You mentioned other important mistakes, which OP would have ran into after rectifying this. So, all is well! :) Nothing cryptic about it
the OP stated Problem is I am getting "Undefined reference to 'printMatrix'". If that doesn't pinpoint the problem ... and which answers are corresponding ... I have no idea what does :)
1

I have fixed your code :

#include <stdio.h> void printMatrix(int matrix[2][3]) { int i = 0, j = 0; for(i = 0 ; i < 2 ; i++) { for(j = 0 ; j < 3 ; j++) { printf("%03d ", matrix[i][j]); } printf("\n"); } printf("\n"); } int main(void) { int matrix[2][3] = {{1, 3, 4}, {2, 0, 1}}; printMatrix(matrix); return 0; } 

The code is not robust and beautiful. Mainly, I have fixed your coding style.

Be careful, the last "element" in the following 'for' loop must not increment i, but j !

for(j=0; j<sizeof matrix[0]/sizeof matrix[0][0];i++){ 

It must be replaced by (or better, by my above code...)

for(j=0; j<sizeof matrix[0]/sizeof matrix[0][0];j++){ 

EDIT :

You can retrieve the size of your matrix :

leni = sizeof matrix / sizeof matrix[0][0]; 

sizeof matrix --> 8

sizeof matrix[0][0] --> 4

leni --> 2

lenj = sizeof matrix[0] / sizeof matrix[0][0] 

sizeof matrix[0] --> 12

sizeof matrix[0][0] --> 4

lenj --> 3

#include <stdio.h> void printMatrix(int matrix[][3]) { int i = 0, j = 0, leni = 0, lenj = 0; leni = sizeof matrix / sizeof matrix[0][0]; lenj = sizeof matrix[0] / sizeof matrix[0][0]; for(i = 0 ; i < leni ; i++) { for(j = 0 ; j < lenj ; j++) { printf("%03d ", matrix[i][j]); } printf("\n"); } printf("\n"); } int main(void) { int matrix[2][3] = {{1, 3, 4}, {2, 0, 1}}; printMatrix(matrix); return 0; } 

5 Comments

Thanks, I have rewritten it badly, I am sorry. I have one more Q, this print is only for matrix[2][3], but can I somehow set print for every kind of a matrix?
Great and one more to go, I am sorry for your time... I am now getting on line void printMatrix(int matrix[][3]) warning of incompatible types... do you know why? I use compilator gcc btw.
I have edited my answer to improve the code. It's not perfect but it works like you want... I think.
it is just warning during compilation with gcc - conflicting types for printMatrix and previous implicit declaration was here... (on line, where I am calling this function from main) I just think I will have to live with it :-) print runs pretty well, thanks
Ok :-) If your code have some warnings, try to fixed it ! It's better for everybody :-)
0

This isn't going to work, for couple of reasons:

(1) Your function signature is wrong:

void printMatrix(int matrix) 

only takes an int, not a matrix

(2) You can't use sizeof inside a function like this, as arrays decay to pointers when passed to functions. You'll need to pass additional parameters in order for the function ot be aware of the matrix dimensions

The cause of the link error you mention however is not apparent from just posting the source code for the function - you'll need to be more explicit about what source files you have and how you're building your code.

4 Comments

@Tadeusz: not always, but yes in this instance it probably does
Correct ... but not an answer to the question :)
@pmg: indeed - not enough info to diagnose the cause of the link error, but it may be moot at this point, since the function is never going to work as it stands anyway. ;-)
sizeof takes either an object or parenthesized type as operand. sizeof doesn't need parenthesis; its the type that needs them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.