Question:
C++ newbie question: can I use an array as an argument to this function?
Antst
2010-10-21 10:34:59 UTC
Hi all:

I have a function defined as:

inline double myFunction( const long int z ) const
{ return C->myFunction[ C->z[ z ] ]; }

and called in main() as:

for ( long int i = 0; i < 10; i++ )
{
for ( long int j = 0; j < 20; j++ )
{
node->myFunction( myMatrix(i,vector[j] ) );
}
}

I want to use a matrix as the argument, but I keep getting a segmentation fault when I try to run the program. I don't understand why. Can anyone help based on the information I have given? I can change the argument, but not the function.

Thanks a lot for any help!
Three answers:
steve
2010-10-21 11:56:05 UTC
const long int z



no, you can not use an array as an argument.



Just to double check, you can change the argument, but not the parameter?
JoelKatz
2010-10-21 10:38:13 UTC
There's no enough code there for us to figure out what's going on. We don't know what 'C' is or what type 'myMatrix' returns. We don't know how 'vector' is allocated. And so on.



It may help to add output lines all over the place. That way, at least you can see the last line of output and that will tell you which line of code is faulting.
2016-11-06 01:59:37 UTC
the form you're calling the function, myMatrix could could be declared like a form of: double *myMatrix[3]; // array of three tips that should double double **myMatrix; // pointer to pointer to double in case you declared it like double myMatrix[3][3]; Then your T form would be interpreted as double*(myMatrix[3]), i.e. A pointer to an array of three doubles. The binary storage of this final assertion is 9 double values, one after yet another. The binary storage that your template function is seeking is a pointer to an array of tips that should T. you may redefine the template function like this: template< typename T, int cols > void matrixCode( T (*someMatrix)[ cols ] ) { }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...