Re: a question of style



questions? wrote:
I am curious about whether I should do this

say I want to calculate dot product of two matrix(4X4,fixed)
I do two ways

1)
void dot_product(double A[4][4] , double B[4][4], double result[4][4]){
blah blah

}

my results are automatically given back in the matrix result[4][4]

or

2)
double [4][4] dot_product(double A[4][4],double B[4][4]){
double result[4][4];
blah blah

return result;
}

for the second type, result is local variable and will be released
after function call, Will the program have problem to access?

Are there better and neat way to this problem?

Thanks

Well since option 2 is not an option at all (arrays are not a first class type in C, so an array cannot be a return value), you're left with option 1.

The other option would be to wrap your array in a struct and return *that*, but that would depend upon how you plan on using this code...

BTW - explicitly mentioning the first dimension of a passed array is superfluous; i.e. `double A[][4]' and `double A[4][4]' -- or for that matter double A[400][4] -- mean the same thing.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
.



Relevant Pages

  • multi-dimensional arrays
    ... License Status: blah blah ... Depending on the data in License Status, I either want to print the ... I split each line into a two part array. ... colon, and the data after the colon. ...
    (comp.lang.awk)
  • Re: "Pseudo-hashes are deprecated" error and accessing a hash of hashes
    ... so I see that this is a Perl 5.8 change. ... > and trying to generate a count of items in a hash of hashes thus: ... > blah blah blah; ... reference to an array. ...
    (comp.lang.perl.misc)
  • Re: PL/I, COBOL, Advantages, Equivalence, et al
    ... as an index in an array. ... type Paint is arrayof Boolean; ... for I in Paint loop ... DEFINE ORDINAL COLOR blah blah blah. ...
    (comp.lang.pl1)
  • Re: Basic ASCII File Reading
    ... I have an ascii file I'm trying to read in with a header and then two ... Blah blah 2 blah blah blah blah ... it but I need to get to the correct position to start reading the ... actually mandate that the input array MUST be allocated to exactly the ...
    (comp.lang.fortran)
  • Re: [PHP] Manually Inserted Row
    ... I'm using PHP to retrieve the values for the manual insert... ... sorting the multidimensional array. ... Dunno why if fails, but don't do that. ...
    (php.general)