0

I'm fairly new to C programming, so bear with me :) I have a main() which generates a 2D-array (aka a matrix). I tried to define an "external" function matrix_multipl(), which takes two matrices from main(), multiplies them and returns them, which of course caused problems (you can't return an array from a function).

So my approach is the following and I want to know whether I understand the process correctly:

I generate the arrays (first[m][n], second[o][p] and outcome[m][p] matrices) inside main and from there, I use only pointers to that array, so if I call the pointer to that array inside matrix_multipl(), it essentially runs back to the main, fetches the respective value from the array, does something with it (matrix multiplications) and finally takes the computed value, runs again to the main() and writes the result into the outcome[m][p] matrix, which stayed all the time inside main() and therefore didn't need to be "returned"?

I hope I got it right, because that sounds absolutely logical and quite useful, because it prevents shoving big arrays back and forth :)

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49

3 Answers3

1

I'm not sure I fully understand what you mean by 'runs back to main()', but yes, what you seem to be talking about is commonly referred to as 'passing by reference', and is a commonly used technique in C programming.

Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • By that I meant that when I use the pointer to first[][] inside matrix_multipl(), the array first[][] is still inside main(), so the function has to look up ("run back") the value inside main() instead of having it "there". – Wojciech Morawiec Jan 02 '13 at 21:59
  • I think I get it - but I think you're confusing the implementation of a function with the location in memory of its local variables. – Carl Norum Jan 02 '13 at 22:01
0

It does not "essentially run back to the main", matrix_multiply() will access the memory pointed to by the matrix pointers.

And, regarding one of the other answers and 'pass by reference', C always uses 'pass by value' - that is the reason for you having to pass pointers rather than the matrices themselves (what is in fact passed to matrix_multiply will be a copy of the pointers you define in main).

Kim Hansson
  • 513
  • 4
  • 12
0

Reference the beginning memory address of the arrays (by reference).

void matrix_multiply(int *array1, int *array2, int *answer) {
// matrix multiply operation.
}

convention:

&array[0] passed into function to satisfy parameter *array

  • I don't understand the "convention" you have at the end - `array` and `&array[0]` are the same in a lot of contexts. Certainly `**array` and `&array[0]` are *not* the same. – Carl Norum Jan 02 '13 at 22:12
  • Further, for non-pointer-arrays (i.e. `type var[n][m]`) this description is not correct. The parameters to the function in this answer are using pointer-arrays; not pointers to arrays, and there *is* a difference. – WhozCraig Jan 02 '13 at 22:15
  • The "convention" still is not correct; `&array[0]` is simply `array+0` which I would hope is clearly just `array`. – WhozCraig Jan 02 '13 at 22:25
  • They both reference the starting address of memory. Been using this for years and gotten to the point where I don't ever need to debug my C programs. Pointers are extremely simple concepts that people make too much of a big deal out of. –  Jan 02 '13 at 22:31
  • Are you claiming for `int array[5]` `*array` is the address of the first element *of that array*? I'm probably not understanding if that is not the case, because `&array[0] == *array` certainly seems to imply that. Either that or perhaps clarity that `&array[0]` in your "convention" is what you're *passing* to a function that has `int *array` as a parameter name, if that was the intention. And regarding a big deal, I hate clutter as much as the next guy. I prefer using `array` rather than `&array[0]` any day. – WhozCraig Jan 02 '13 at 22:37
  • I should have just said that one would satisfy the function's requirement for a pointer (in this case `*array`) by inserting `&array[0]` . I just view them both as just referencing an address. –  Jan 02 '13 at 22:42
  • Clutter reduction is good, but for illustrative purposes since '&' is the notation for address I thought it be more educational to say that one simply gets the address of the first element. –  Jan 02 '13 at 22:45
  • @CarlNorum he meant what is in the description now, which (oddly) i understand. – WhozCraig Jan 02 '13 at 22:47
  • Not if you use it as a declaration of a functions parameter. As a parameter it means simply a pointer. –  Jan 02 '13 at 22:47
  • I think I see what you're getting at now. That's not valid syntax without the type, which is confusing. – Carl Norum Jan 02 '13 at 22:57