+ 2
Challenge: write a function that returns a matrix of function pointers
Weird challenge :) Look my solution: https://code.sololearn.com/cfxZARGS1AR3/#cpp
17 Réponses
+ 4
@Andrea
What is a matrix?
This right? - int matrix[3][3];
This one is the same as int ** matrix, as an array is nothing but a pointer. And a pointer to a pointer == 2D Array...
Or this? - https://code.sololearn.com/cq24J951TxgI/?ref=app
BTW how will you assign functions to the matrix of functions?
As input won't work...
So you'll have to do it manually...
Here is something I can think of. Please guide me...
typedef int(*fx)(int,int)** FxMatrix;
typedef int(*fx)(int,int) FxType;
FxMatrix Assign( int(*fx1)(int,int), int(*fx2)(int,int), int(*fx3)(int,int), int(*fx4)(int,int))
{
FxMatrix mat={{fx1, fx2},
{fx3, fx4}};
return mat;
}
int add(int a, int b)
{ return a+b; }
int sub(int a, int b)
{ return a-b; }
int mul(int a, int b)
{ return a*b; }
int div(int a, int b)
{ return a/b; }
int main()
{
FxMatrix matrix = new FxType*[2];
for(int i=0;i<2;i++) matrix[i] = new FxType[2];
matrix = Assign( &add, &sub, &mul, &div );
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
cout<<matrix[i][j](2,2);
//Prints 4,0,4,1...
}
+ 2
@kinshuk i dont know with your method. I dont use vector, instead i use dear old malloc()
+ 2
@kinshuk
you said too much time pointers 😂😂😂
but no...surely not like this
remember that matrix are not pointer to pointers and rembember that someone invented typedef word
+ 2
@kinshuk
I think your class is great but now i can't control 2300 lines.
You make a mistake: array are not pointers and matrix are not pointers to pointers. What? http://c-faq.com/aryptr/aryptrequiv.html
"A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T."
Now if an array decays in a pointer....a matrix?
Try yourself:
int matrix[10][20];
int **P = matrix; // Error: cannot convert a int(*)[20] into a (int**)
int (*p)[20] = matrix; //Ok
What does it mean? It's difficult to explain for me in english but matrix is an pointer-to-matrix that automaticaly decays into a pointer-to-array of 20 elements.
So matrix points first 20 elements.
When you create a matrix using pointers-to-pointers you have to do something that using pointers-to-array you have not to do: create an array of pointers.
Because the pointers-to-array has an information that the pointer-to-pointer has not: number 20.
Look my code and how its simple create a matrix using only one pointer-to-array. There is a little example. Then i will post another code for explain my solution for this challenge.
However your little code is not so bad, but you create it in the main: I want a function that create and return a matrix to the main ;)
P.S. typedef int(*fx)(int,int)** FxMatrix; is not ok for the compiler :P
+ 2
@kinshuk
Yes automatically an array decays into a pointer to its first element, specially when it is an argument of a function ;)
If you want, look my solution here: https://code.sololearn.com/cfxZARGS1AR3/#cpp
+ 2
Though I didn't win this challenge, I learned many new things.
Thank You very much!
https://code.sololearn.com/c10itmF1cL6V/?ref=app
+ 2
Thanks for playing!!!
+ 2
@Andrea
Waiting for the next challenge.
I will try to win that one!
+ 2
New challenge is coming 😂
+ 2
New challenge is ready 😃
https://www.sololearn.com/Discuss/468186/?ref=app
+ 1
Yes it is 😄
+ 1
a matrix of function pointers?
Something like this?
vector<vector<int(*func)(int,int)>> ?
How will one assign functions to this?
+ 1
I have no idea how you declare a pointer of pointers to a pointer to a function.
Like this maybe?
int (*func)(int,int) ** function_array;
+ 1
@Andrea
Yeah since the typedef if not working, I converted it to a define, and that works...
I had read that a function which has an array as an argument:
int func(int arr[10])
reads it like this:
int func(int *arr)
Maybe Im confused. Ill have to study the tutorials again...
+ 1
@Andrea
Im returning the matrix in main.
How can you simply print a matrix of function pointers without saving it in another variable of the same type, after all its a matrix!
You want me to use print function?
How will that return a matrix?
I can think of this:
void print(FxMatrix a);
In main:
print(assign(...));
//Returns a rvalue ref which is used by print...
+ 1
@Andrea
Youre doing the same thing -_-
Returning a matrix in main to a matrix variable...
0
Sounds interesting...