Hi all,
it is possible to declere a function within a structure, then in the main function with the use of the pointers call this function and assign the specific parameters . To make my idea clearer check out the sample code.

struct {
       int AND_function;
       int math_funct;
       int comp_funct;
       int regul_funct;
}functions;

AND_function (parameter1, parameter2, . . .)
{
                     .
                     .
                     .
}

int main()
{
     struct functions *fun;
     function.AND_function(par.parameter1, par.parameter);
}

Thanks for your help,

Best regards,

BoSCHoW.

Recommended Answers

All 4 Replies

It looks like you want an object:

struct functions {
  int AND_function ( int lhs, int rhs )
  {
    // ...
  }
};

int main()
{
  functions fun;
  fun.AND_function( 5, 10 );
}

What exactly are you trying to do?

First of all I am working in C, because i am trying to program a microchip and so a lot of people said that C is better then C++ because is more like assembler. Since my project is quite large i will have to use both C and C++. I am currently using C for the microchip, C++ for some kind of communication between human and this microchip, some type of interface. For now i am focusing on the microchip . . .
The idea is that if i write the symbol & the microchip will use the AND function . . . . now to get things a little messy if the user writes input1 & input2 this means the parameters of the function are input 1 & input2 . . . so the parameter have to be "flexible"... i hope i made myself clear . . .

Best regards,
BoSCHoW

>First of all I am working in C
Hmm, then maybe you should have asked in the C forum.

>C is better then C++ because is more like assembler
What a load of crap. Now, if your processor doesn't support a C++ compiler, or the C++ compiler is weak, that's a good excuse to use C. If you don't use any of the features of C++ not present in C, that's a good excuse to use C. Because some bozo told you C is better, that's not a good excuse.

>i hope i made myself clear . . .
Not really. I still have no clue what you're trying to do, and it sounds a bit like you want operator overloading. However, the C equivalent to the C++ code I gave you is:

struct functions {
  int (*AND_function) ( int lhs, int rhs );
};

int AND_function_impl ( int lhs, int rhs )
{
  /* ... */
}

int main()
{
  struct functions fun = {&AND_function_impl};
  fun.AND_function( 5, 10 );
}

http://richardbowles.tripod.com/cpp/cpp19.htm

>First of all I am working in C
Hmm, then maybe you should have asked in the C forum.

>C is better then C++ because is more like assembler
What a load of crap. Now, if your processor doesn't support a C++ compiler, or the C++ compiler is weak, that's a good excuse to use C. If you don't use any of the features of C++ not present in C, that's a good excuse to use C. Because some bozo told you C is better, that's not a good excuse.

>i hope i made myself clear . . .
Not really. I still have no clue what you're trying to do, and it sounds a bit like you want operator overloading. However, the C equivalent to the C++ code I gave you is:

struct functions {
  int (*AND_function) ( int lhs, int rhs );
};

int AND_function_impl ( int lhs, int rhs )
{
  /* ... */
}

int main()
{
  struct functions fun = {&AND_function_impl};
  fun.AND_function( 5, 10 );
}
commented: You bumped a 3.5 year old thread for that?? -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.