Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Array of pointer-to-functions (http://www.velocityreviews.com/forums/t952282-array-of-pointer-to-functions.html)

 I 09-16-2012 11:25 PM

Array of pointer-to-functions

Hello,

Question: How do I create a n array of pointer - to - functions to be filled by a user?
My goal is to create the calculate function, which takes two values and passes them to a array of pointer-to-functions, which calculates something from those 2 values and returns it.

I am not getting any build errors. It is definitly not running though.

I am guessing my problem is in one of 3 places:
1.) the calculate protoype
2.) the calculate function
3.) the defenition of double calculate(double y, double x, int z,double (*pt[])(double a,double b))
but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.

Thanks
I

Here is my code:

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <limits>

using namespace std;
//int size;
//functions below here
double calculate(double y, double x, int z,double (*pt[])(double a,double b)); //changed 4 to [] in hopes of creating a blank array
double subtract(double a, double b);
double mult(double a, double b);
double divide(double a, double b);
double mean(double a, double b);
double pythag(double a, double b);
/*
* begin main
*/
int main()
{
int choice,size_choice;
double a,b,test;
cout<<"Enter two values: \n";
if(!(cin>>a>>b))
cout<<"Catastrophic Error!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
//Now going to attempt to write a switch that allow users to choose up to 5 functions to operate on their numbers
cout<<"Choose the # of functions you wish to use (only six functions currently available) \n";
while(1)
{
if(!(cin>>size_choice))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n') ;
cout<<"Failure\n";
}
else
break;
}
"1.) add 2.) subtract 3.) mult\n"
"4.) divide 5.) mean 6.) pythag\n";
//cin>>choice;
for(int i=0;i<size_choice;i++)
{
cin>>choice;
switch(choice)
{
break;
case 2: pt[i]=subtract;
break;
case 3: pt[i]=mult;
break;
case 4: pt[i]=divide;
break;
case 5: pt[i]=mean;
break;
case 6: pt[i]=pythag;
break;
}
}
calculate(a,b,size_choice,pt);
return 0;
}
/*
*/
double calculate(double y, double x,int z, double (*pt[])(double,double))
{
double * temp = new double [z];
//double a,b,test=0;
for(int i=0;i<z;i++)
{
temp[i]=(*pt[i])(x,y);
cout<<temp[i]<<" next \n";
}

delete [] temp;
}
{
return a+b;
}
double subtract(double a, double b)
{
double total=a-b;
}
double mult(double a, double b)
{
return a*b;
}
double divide(double a, double b)
{

double total=a/b;
//double remainder=a%b;
}
double mean(double a, double b)
{
return (a+b)/2;
}
double pythag(double a, double b)
{
return sqrt((a*a)+(b*b));
}

 s0suk3@gmail.com 09-17-2012 03:11 AM

Re: Array of pointer-to-functions

On Sunday, September 16, 2012 6:25:52 PM UTC-5, I wrote:
> Hello,
>
>
>
> Question: How do I create a n array of pointer - to - functions to be filled by a user?
>
> My goal is to create the calculate function, which takes two values and passes them to a array of pointer-to-functions, which calculates something from those 2 values and returns it.
>
>
>
> I am not getting any build errors. It is definitly not running though.
>
>
>
> I am guessing my problem is in one of 3 places:
>
> 1.) the calculate protoype
>
> 2.) the calculate function
>
> 3.) the defenition of double calculate(double y, double x, int z,double (*pt[])(double a,double b))
>
> but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.
>
>
>
> Thanks
>
> I
>
>
>
> Here is my code:
>
>
>
> #include <cstdlib>
>
> #include <iostream>
>
> #include <cstring>
>
> #include <cmath>
>
> #include <limits>
>
>
>
> using namespace std;
>
> //int size;
>
> //functions below here
>
> double calculate(double y, double x, int z,double (*pt[])(double a,doubleb)); //changed 4 to [] in hopes of creating a blank array
>
> double add(double a, double b);
>
> double subtract(double a, double b);
>
> double mult(double a, double b);
>
> double divide(double a, double b);
>
> double mean(double a, double b);
>
> double pythag(double a, double b);
>
> /*
>
> * begin main
>
> */
>
> int main()
>
> {
>
> int choice,size_choice;
>
> double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};

The problem is here. This is not valid (the size of an array must be a constant), although some compilers accept it. But even with those compilers that accept it, it won't do what you want because size_choice hasn't been initialized. Most likely it will contain a random large value, and the program will try to allocate a large amount of memory on the stack, causing a stackoverflow.

> double a,b,test;
>
> cout<<"Enter two values: \n";
>
> if(!(cin>>a>>b))
>
> cout<<"Catastrophic Error!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
>
> //Now going to attempt to write a switch that allow users to choose up to 5 functions to operate on their numbers
>
> cout<<"Choose the # of functions you wish to use (only six functions currently available) \n";
>
> while(1)
>
> {
>
> if(!(cin>>size_choice))
>
> {
>
> cin.clear();
>
> cin.ignore(numeric_limits<streamsize>::max(),'\n') ;
>
> cout<<"Failure\n";
>
> }
>
> else
>
> break;
>
> }

Here is where you would declare the array, now that size_choice has been initialized:

double (*pt[size_choice])(double a, double b);// {add, subtract, mult, divide};

Or use std::vector:

vector<double(*)(double, double)> pt(size_choice);

But I don't see why you need to allocate only size_choice pointers. Just declare an array that contains all the function pointers and use only the ones you need.

>
> "1.) add 2.) subtract 3.) mult\n"
>
> "4.) divide 5.) mean 6.) pythag\n";
>
> //cin>>choice;
>
> for(int i=0;i<size_choice;i++)
>
> {
>
> cin>>choice;
>
> switch(choice)
>
> {
>
>
> break;
>
> case 2: pt[i]=subtract;
>
> break;
>
> case 3: pt[i]=mult;
>
> break;
>
> case 4: pt[i]=divide;
>
> break;
>
> case 5: pt[i]=mean;
>
> break;
>
> case 6: pt[i]=pythag;
>
> break;
>
> }
>
> }
>
> calculate(a,b,size_choice,pt);
>
> return 0;
>
> }
>
> /*
>
> *Define your functions after break!
>
> */
>
> double calculate(double y, double x,int z, double (*pt[])(double,double))
>
> {
>
> double * temp = new double [z];
>
> //double a,b,test=0;
>
> for(int i=0;i<z;i++)
>
> {
>
> temp[i]=(*pt[i])(x,y);
>
> cout<<temp[i]<<" next \n";
>
> }
>
>
>
> delete [] temp;
>
> }
>
> double add(double a, double b)
>
> {
>
> return a+b;
>
> }
>
> double subtract(double a, double b)
>
> {
>
> double total=a-b;
>
>
> }
>
> double mult(double a, double b)
>
> {
>
> return a*b;
>
> }
>
> double divide(double a, double b)
>
> {
>
>
>
> double total=a/b;
>
> //double remainder=a%b;
>
>
> }
>
> double mean(double a, double b)
>
> {
>
> return (a+b)/2;
>
> }
>
> double pythag(double a, double b)
>
> {
>
> return sqrt((a*a)+(b*b));
>
> }

By the way, a better way to do this is with polymorphism:

class Operation
{
public:
virtual double Calculate(double a, double b) = 0;
};

{
public:
double Calculate(double a, double b) { return a + b; }
};

class SubtractOperation : public Operation
{
public:
double Calculate(double a, double b) { return a - b; }
};

....

Then create a vector of Operations:

vector<Operation*> operations(size_choice);

And add objects to it like you added function pointers to the other array:

switch (choice)
{
case 1:
break;
case 2:
operations[i] = new SubtractOperation();
break;
....
}

And then call Calculate on all the objects:

for (int i = 0; i < n; ++i)
cout << operations[i]->Calculate(a, b) << endl;

 I 09-17-2012 03:57 AM

Re: Array of pointer-to-functions

On Sunday, September 16, 2012 10:11:33 PM UTC-5, (unknown) wrote:
> On Sunday, September 16, 2012 6:25:52 PM UTC-5, I wrote:
>
> > Hello,

>
> >

>
> >

>
> >

>
> > Question: How do I create a n array of pointer - to - functions to be filled by a user?

>
> >

>
> > My goal is to create the calculate function, which takes two values andpasses them to a array of pointer-to-functions, which calculates somethingfrom those 2 values and returns it.

>
> >

>
> >

>
> >

>
> > I am not getting any build errors. It is definitly not running though.

>
> >

>
> >

>
> >

>
> > I am guessing my problem is in one of 3 places:

>
> >

>
> > 1.) the calculate protoype

>
> >

>
> > 2.) the calculate function

>
> >

>
> > 3.) the defenition of double calculate(double y, double x, int z,double(*pt[])(double a,double b))

>
> >

>
> > but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.

>
> >

>
> >

>
> >

>
> > Thanks

>
> >

>
> > I

>
> >

>
> >

>
> >

>
> > Here is my code:

>
> >

>
> >

>
> >

>
> > #include <cstdlib>

>
> >

>
> > #include <iostream>

>
> >

>
> > #include <cstring>

>
> >

>
> > #include <cmath>

>
> >

>
> > #include <limits>

>
> >

>
> >

>
> >

>
> > using namespace std;

>
> >

>
> > //int size;

>
> >

>
> > //functions below here

>
> >

>
> > double calculate(double y, double x, int z,double (*pt[])(double a,double b)); //changed 4 to [] in hopes of creating a blank array

>
> >

>
> > double add(double a, double b);

>
> >

>
> > double subtract(double a, double b);

>
> >

>
> > double mult(double a, double b);

>
> >

>
> > double divide(double a, double b);

>
> >

>
> > double mean(double a, double b);

>
> >

>
> > double pythag(double a, double b);

>
> >

>
> > /*

>
> >

>
> > * begin main

>
> >

>
> > */

>
> >

>
> > int main()

>
> >

>
> > {

>
> >

>
> > int choice,size_choice;

>
> >

>
> > double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};

>
>
>
> The problem is here. This is not valid (the size of an array must be a constant), although some compilers accept it. But even with those compilers that accept it, it won't do what you want because size_choice hasn't been initialized. Most likely it will contain a random large value, and the program will try to allocate a large amount of memory on the stack, causing a stack overflow.
>
>
>
> > double a,b,test;

>
> >

>
> > cout<<"Enter two values: \n";

>
> >

>
> > if(!(cin>>a>>b))

>
> >

>
> > cout<<"Catastrophic Error!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

>
> >

>
> > //Now going to attempt to write a switch that allow users to chooseup to 5 functions to operate on their numbers

>
> >

>
> > cout<<"Choose the # of functions you wish to use (only six functions currently available) \n";

>
> >

>
> > while(1)

>
> >

>
> > {

>
> >

>
> > if(!(cin>>size_choice))

>
> >

>
> > {

>
> >

>
> > cin.clear();

>
> >

>
> > cin.ignore(numeric_limits<streamsize>::max(),'\n') ;

>
> >

>
> > cout<<"Failure\n";

>
> >

>
> > }

>
> >

>
> > else

>
> >

>
> > break;

>
> >

>
> > }

>
>
>
> Here is where you would declare the array, now that size_choice has been initialized:
>
>
>
> double (*pt[size_choice])(double a, double b);// {add, subtract, mult, divide};
>
>
>
> Or use std::vector:
>
>
>
> vector<double(*)(double, double)> pt(size_choice);
>
>
>
> But I don't see why you need to allocate only size_choice pointers. Just declare an array that contains all the function pointers and use only the ones you need.
>
>
>
> > cout<<"Choose your functions: \n"

>
> >

>
> > "1.) add 2.) subtract 3.) mult\n"

>
> >

>
> > "4.) divide 5.) mean 6.) pythag\n";

>
> >

>
> > //cin>>choice;

>
> >

>
> > for(int i=0;i<size_choice;i++)

>
> >

>
> > {

>
> >

>
> > cin>>choice;

>
> >

>
> > switch(choice)

>
> >

>
> > {

>
> >

>

>
> >

>
> > break;

>
> >

>
> > case 2: pt[i]=subtract;

>
> >

>
> > break;

>
> >

>
> > case 3: pt[i]=mult;

>
> >

>
> > break;

>
> >

>
> > case 4: pt[i]=divide;

>
> >

>
> > break;

>
> >

>
> > case 5: pt[i]=mean;

>
> >

>
> > break;

>
> >

>
> > case 6: pt[i]=pythag;

>
> >

>
> > break;

>
> >

>
> > }

>
> >

>
> > }

>
> >

>
> > calculate(a,b,size_choice,pt);

>
> >

>
> > return 0;

>
> >

>
> > }

>
> >

>
> > /*

>
> >

>
> > *Define your functions after break!

>
> >

>
> > */

>
> >

>
> > double calculate(double y, double x,int z, double (*pt[])(double,double))

>
> >

>
> > {

>
> >

>
> > double * temp = new double [z];

>
> >

>
> > //double a,b,test=0;

>
> >

>
> > for(int i=0;i<z;i++)

>
> >

>
> > {

>
> >

>
> > temp[i]=(*pt[i])(x,y);

>
> >

>
> > cout<<temp[i]<<" next \n";

>
> >

>
> > }

>
> >

>
> >

>
> >

>
> > delete [] temp;

>
> >

>
> > }

>
> >

>
> > double add(double a, double b)

>
> >

>
> > {

>
> >

>
> > return a+b;

>
> >

>
> > }

>
> >

>
> > double subtract(double a, double b)

>
> >

>
> > {

>
> >

>
> > double total=a-b;

>
> >

>

>
> >

>
> > }

>
> >

>
> > double mult(double a, double b)

>
> >

>
> > {

>
> >

>
> > return a*b;

>
> >

>
> > }

>
> >

>
> > double divide(double a, double b)

>
> >

>
> > {

>
> >

>
> >

>
> >

>
> > double total=a/b;

>
> >

>
> > //double remainder=a%b;

>
> >

>

>
> >

>
> > }

>
> >

>
> > double mean(double a, double b)

>
> >

>
> > {

>
> >

>
> > return (a+b)/2;

>
> >

>
> > }

>
> >

>
> > double pythag(double a, double b)

>
> >

>
> > {

>
> >

>
> > return sqrt((a*a)+(b*b));

>
> >

>
> > }

>
>
>
> By the way, a better way to do this is with polymorphism:
>
>
>
> class Operation
>
> {
>
> public:
>
> virtual double Calculate(double a, double b) = 0;
>
> };
>
>
>
> class AddOperation : public Operation
>
> {
>
> public:
>
> double Calculate(double a, double b) { return a + b; }
>
> };
>
>
>
> class SubtractOperation : public Operation
>
> {
>
> public:
>
> double Calculate(double a, double b) { return a - b; }
>
> };
>
>
>
> ...
>
>
>
> Then create a vector of Operations:
>
>
>
> vector<Operation*> operations(size_choice);
>
>
>
> And add objects to it like you added function pointers to the other array:
>
>
>
> switch (choice)
>
> {
>
> case 1:
>
>
> break;
>
> case 2:
>
> operations[i] = new SubtractOperation();
>
> break;
>
> ...
>
> }
>
>
>
> And then call Calculate on all the objects:
>
>
>
> for (int i = 0; i < n; ++i)
>
> cout << operations[i]->Calculate(a, b) << endl;

On Sunday, September 16, 2012 10:11:33 PM UTC-5, (unknown) wrote:
> On Sunday, September 16, 2012 6:25:52 PM UTC-5, I wrote:
>
> > Hello,

>
> >

>
> >

>
> >

>
> > Question: How do I create a n array of pointer - to - functions to be filled by a user?

>
> >

>
> > My goal is to create the calculate function, which takes two values andpasses them to a array of pointer-to-functions, which calculates somethingfrom those 2 values and returns it.

>
> >

>
> >

>
> >

>
> > I am not getting any build errors. It is definitly not running though.

>
> >

>
> >

>
> >

>
> > I am guessing my problem is in one of 3 places:

>
> >

>
> > 1.) the calculate protoype

>
> >

>
> > 2.) the calculate function

>
> >

>
> > 3.) the defenition of double calculate(double y, double x, int z,double(*pt[])(double a,double b))

>
> >

>
> > but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.

>
> >

>
> >

>
> >

>
> > Thanks

>
> >

>
> > I

>
> >

>
> >

>
> >

>
> > Here is my code:

>
> >

>
> >

>
> >

>
> > #include <cstdlib>

>
> >

>
> > #include <iostream>

>
> >

>
> > #include <cstring>

>
> >

>
> > #include <cmath>

>
> >

>
> > #include <limits>

>
> >

>
> >

>
> >

>
> > using namespace std;

>
> >

>
> > //int size;

>
> >

>
> > //functions below here

>
> >

>
> > double calculate(double y, double x, int z,double (*pt[])(double a,double b)); //changed 4 to [] in hopes of creating a blank array

>
> >

>
> > double add(double a, double b);

>
> >

>
> > double subtract(double a, double b);

>
> >

>
> > double mult(double a, double b);

>
> >

>
> > double divide(double a, double b);

>
> >

>
> > double mean(double a, double b);

>
> >

>
> > double pythag(double a, double b);

>
> >

>
> > /*

>
> >

>
> > * begin main

>
> >

>
> > */

>
> >

>
> > int main()

>
> >

>
> > {

>
> >

>
> > int choice,size_choice;

>
> >

>
> > double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};

>
>
>
> The problem is here. This is not valid (the size of an array must be a constant), although some compilers accept it. But even with those compilers that accept it, it won't do what you want because size_choice hasn't been initialized. Most likely it will contain a random large value, and the program will try to allocate a large amount of memory on the stack, causing a stack overflow.
>
>
>
> > double a,b,test;

>
> >

>
> > cout<<"Enter two values: \n";

>
> >

>
> > if(!(cin>>a>>b))

>
> >

>
> > cout<<"Catastrophic Error!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

>
> >

>
> > //Now going to attempt to write a switch that allow users to chooseup to 5 functions to operate on their numbers

>
> >

>
> > cout<<"Choose the # of functions you wish to use (only six functions currently available) \n";

>
> >

>
> > while(1)

>
> >

>
> > {

>
> >

>
> > if(!(cin>>size_choice))

>
> >

>
> > {

>
> >

>
> > cin.clear();

>
> >

>
> > cin.ignore(numeric_limits<streamsize>::max(),'\n') ;

>
> >

>
> > cout<<"Failure\n";

>
> >

>
> > }

>
> >

>
> > else

>
> >

>
> > break;

>
> >

>
> > }

>
>
>
> Here is where you would declare the array, now that size_choice has been initialized:
>
>
>
> double (*pt[size_choice])(double a, double b);// {add, subtract, mult, divide};
>
>
>
> Or use std::vector:
>
>
>
> vector<double(*)(double, double)> pt(size_choice);
>
>
>
> But I don't see why you need to allocate only size_choice pointers. Just declare an array that contains all the function pointers and use only the ones you need.
>
>
>
> > cout<<"Choose your functions: \n"

>
> >

>
> > "1.) add 2.) subtract 3.) mult\n"

>
> >

>
> > "4.) divide 5.) mean 6.) pythag\n";

>
> >

>
> > //cin>>choice;

>
> >

>
> > for(int i=0;i<size_choice;i++)

>
> >

>
> > {

>
> >

>
> > cin>>choice;

>
> >

>
> > switch(choice)

>
> >

>
> > {

>
> >

>

>
> >

>
> > break;

>
> >

>
> > case 2: pt[i]=subtract;

>
> >

>
> > break;

>
> >

>
> > case 3: pt[i]=mult;

>
> >

>
> > break;

>
> >

>
> > case 4: pt[i]=divide;

>
> >

>
> > break;

>
> >

>
> > case 5: pt[i]=mean;

>
> >

>
> > break;

>
> >

>
> > case 6: pt[i]=pythag;

>
> >

>
> > break;

>
> >

>
> > }

>
> >

>
> > }

>
> >

>
> > calculate(a,b,size_choice,pt);

>
> >

>
> > return 0;

>
> >

>
> > }

>
> >

>
> > /*

>
> >

>
> > *Define your functions after break!

>
> >

>
> > */

>
> >

>
> > double calculate(double y, double x,int z, double (*pt[])(double,double))

>
> >

>
> > {

>
> >

>
> > double * temp = new double [z];

>
> >

>
> > //double a,b,test=0;

>
> >

>
> > for(int i=0;i<z;i++)

>
> >

>
> > {

>
> >

>
> > temp[i]=(*pt[i])(x,y);

>
> >

>
> > cout<<temp[i]<<" next \n";

>
> >

>
> > }

>
> >

>
> >

>
> >

>
> > delete [] temp;

>
> >

>
> > }

>
> >

>
> > double add(double a, double b)

>
> >

>
> > {

>
> >

>
> > return a+b;

>
> >

>
> > }

>
> >

>
> > double subtract(double a, double b)

>
> >

>
> > {

>
> >

>
> > double total=a-b;

>
> >

>

>
> >

>
> > }

>
> >

>
> > double mult(double a, double b)

>
> >

>
> > {

>
> >

>
> > return a*b;

>
> >

>
> > }

>
> >

>
> > double divide(double a, double b)

>
> >

>
> > {

>
> >

>
> >

>
> >

>
> > double total=a/b;

>
> >

>
> > //double remainder=a%b;

>
> >

>

>
> >

>
> > }

>
> >

>
> > double mean(double a, double b)

>
> >

>
> > {

>
> >

>
> > return (a+b)/2;

>
> >

>
> > }

>
> >

>
> > double pythag(double a, double b)

>
> >

>
> > {

>
> >

>
> > return sqrt((a*a)+(b*b));

>
> >

>
> > }

>
>
>
> By the way, a better way to do this is with polymorphism:
>
>
>
> class Operation
>
> {
>
> public:
>
> virtual double Calculate(double a, double b) = 0;
>
> };
>
>
>
> class AddOperation : public Operation
>
> {
>
> public:
>
> double Calculate(double a, double b) { return a + b; }
>
> };
>
>
>
> class SubtractOperation : public Operation
>
> {
>
> public:
>
> double Calculate(double a, double b) { return a - b; }
>
> };
>
>
>
> ...
>
>
>
> Then create a vector of Operations:
>
>
>
> vector<Operation*> operations(size_choice);
>
>
>
> And add objects to it like you added function pointers to the other array:
>
>
>
> switch (choice)
>
> {
>
> case 1:
>
>
> break;
>
> case 2:
>
> operations[i] = new SubtractOperation();
>
> break;
>
> ...
>
> }
>
>
>
> And then call Calculate on all the objects:
>
>
>
> for (int i = 0; i < n; ++i)
>
> cout << operations[i]->Calculate(a, b) << endl;

Great, thanks for the tip! Just getting into classes, hopefully polymorphisms will come up in the text.

 s0suk3@gmail.com 09-17-2012 04:21 AM

Re: Array of pointer-to-functions

On Sunday, September 16, 2012 10:57:08 PM UTC-5, I wrote:
> Great, thanks for the tip! Just getting into classes, hopefully polymorphisms will come up in the text.

More generally, don't declare all variables at the top of a function. Declare them right where they are used. This is not just about correctness (like in this case), but also about readability and style.

 Werner 09-17-2012 12:29 PM

Re: Array of pointer-to-functions

On Monday, September 17, 2012 1:25:52 AM UTC+2, I wrote:
> Hello,
>
>
>
> Question: How do I create a n array of pointer - to - functions to be filled by a user?

that would be (for C++11):

std::vector<void(*)(double,double)> functions =
{
add, subtract, mult, divide, mean, pythag
};

or

void (*farray[]) (double, double) =
{
add, subtract, mult, divide, mean, pythag
};

if( choice < farray.size() )
{
calculate (...farray[choice] );
}

Why the switch statement?

Kind regards,

Werner

 Luca Risolia 09-17-2012 02:27 PM

Re: Array of pointer-to-functions

On 17/09/2012 01:25, I wrote:
> I am not getting any build errors. It is definitly not running though.

That's probably because there is one bug in your code at least (see below).

> 3.) the defenition of double calculate(double y, double x, int z,double (*pt[])(double a,double b))
> but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.

> Here is my code:

> int main()
> {
> int choice,size_choice;

size_choice is uninitialized there. It may hold a non-positive value.

> double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};

When size_choice is <= 0, the above statement leads to Undefined Behaviour.

 I 09-17-2012 04:10 PM

Re: Array of pointer-to-functions

> Why the switch statement?
>
>
>
> Kind regards,
>
>
>
> Werner

I took a problem from C++ Primer Plus and tried to up the degree of difficulty, The original goal was to use a pointer to function to some function like add(). I wanted to see if I could set up a situation that would allow a user to select multiple functions of their choosing. Switch is the best wayI currently know.

Ian

 I 09-17-2012 04:12 PM

Re: Array of pointer-to-functions

On Monday, September 17, 2012 9:27:14 AM UTC-5, Luca Risolia wrote:
> On 17/09/2012 01:25, I wrote:
>
> > I am not getting any build errors. It is definitly not running though.

>
>
>
> That's probably because there is one bug in your code at least (see below).
>
>
>
> > 3.) the defenition of double calculate(double y, double x, int z,double (*pt[])(double a,double b))

>
> > but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.

>
>
>
> > Here is my code:

>
>
>
> > int main()

>
> > {

>
> > int choice,size_choice;

>
>
>
> size_choice is uninitialized there. It may hold a non-positive value.
>
>
>
> > double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};

>
>
>
> When size_choice is <= 0, the above statement leads to Undefined Behaviour.

As soon as I moved double (*pt[size_choice])(double a, double b) below size_choice initialization it worked fine. Thanks!

 s0suk3@gmail.com 09-17-2012 05:48 PM

Re: Array of pointer-to-functions

On Monday, September 17, 2012 8:57:00 AM UTC-5, Scott Lurndal wrote:
> s0suk3@gmail.com writes:
>
> >On Sunday, September 16, 2012 10:57:08 PM UTC-5, I wrote:

>
> >> Great, thanks for the tip! Just getting into classes, hopefully polymorphisms will come up in the text.

>
> >

>
> >More generally, don't declare all variables at the top of a function. Declare them right where they are used. This is not just about correctness (like in this case), but also about readability and style.

>
>
>
>
>
> Which is a matter of opinion. I prefer all variables to be declared at the beginning of a basic-block. Makes it
>
> easier to find when functions get larger than a handful of lines. Also prevents compile errors in the presence
>
> of goto's (which can be used safely and properly, primarily as early exits w/cleanup).

What use is "finding" a variable, when you don't know how it will be used? Declaring variables too far from where they are used makes you jump back and forth trying to see where it's declared and where it's actually used.

There are other points, and this is actually a FAQ: http://www.parashift.com/c++-faq-lit...first-use.html

 Ian Collins 09-17-2012 08:06 PM

Re: Array of pointer-to-functions

On 09/18/12 01:57 AM, Scott Lurndal wrote:
> s0suk3@gmail.com writes:
>> On Sunday, September 16, 2012 10:57:08 PM UTC-5, I wrote:
>>> Great, thanks for the tip! Just getting into classes, hopefully polymorphisms will come up in the text.

>>
>> More generally, don't declare all variables at the top of a function. Declare them right where they are used. This is not just about correctness (like in this case), but also about readability and style.

>
>
> Which is a matter of opinion. I prefer all variables to be declared at the beginning of a basic-block. Makes it
> easier to find when functions get larger than a handful of lines. Also prevents compile errors in the presence
> of goto's (which can be used safely and properly, primarily as early exits w/cleanup).

I smell Troll bait

We all know functions shouldn't be longer than a handful of lines and no
C++ programmer would ever risk his or her head by using goto.

--
Ian Collins

All times are GMT. The time now is 05:07 PM.