Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Vote on your favorite hash algorithm here!

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-27-2007, 10:30 AM
sowmi sowmi is offline
Newbie
 
Join Date: Jun 2007
Posts: 5
Credits: 0
Rep Power: 0
sowmi is on a distinguished road
Default Reg Fucntion pointers

Hi All,

I am trying to implement the concept of function pointer in C++.
While compiling i got some errors which I couldnt figure out the reason .
So,Please let me know what changes should be done for resolving that error.

I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.


Below are my code,


Code:
command.h
**********
#define FUNCPTR  void *
#define MAX_NUM_COMMANDS 3

typedef struct
{
   char command[LINELEN];                           /* name of the command */
   char parm1[LINELEN];                             /*parameter 1 */
   char parm2[LINELEN];                             /*parameter 2 */
                                      
} ExeCmd_s;
class FunctionEntry
{

public:
    char* commandString;
    FUNCPTR pFun;   
    int SearchCommand(char* command);
};
FunctionEntry funArr[MAX_NUM_COMMANDS]=
{
	    {"aaa",(FUNCPTR)aaa},
            {"bbb", (FUNCPTR)bbb},
   	    {"ccc",(FUNCPTR)ccc}

};
enum commandName
{
	aaa = 0,
	bbb
};


command.c
**********
void void Execute ()
{
  .........
  .........
 Execmd execmd;
 FunctionEntry command;
 int rc;
 if(0 != SearchCommand(exeCmd.command))
 {
    command = FunctionEntry.SearchCommand(exeCmd.command);
    switch((int)command.numOfArguements)
    {
         case Zero :
           rc = ((command.pFun)();
           break;
	 case one:
	   rc = ((command.pFun),exeCmd.parm1);
	   break;
    }
 }
 else
 {
   printf("No match");
 }

}



//Function to search and returns the index of the command

int FunctionEntry :: SearchCommand(char* command)
{
     
     ExeCmd_s exeCmd;  //already had a Structure where I am extracting the commandname..
     int index = 0;     
     while(index < MAX_NUM_COMMANDS)
     {
	 if(strcmp(exeCmd.command,funArr[index]) == 0)
	 {
	    return &funArr[index];
	 }
         index++;	
    }
     return NULL;
}



Please let me know where i should change the code.
Expecting some solutions for this code ASAP .

Thanks,
Sowmi

Last edited by WingedPanther; 06-27-2007 at 10:58 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-27-2007, 11:02 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,535
Last Blog:
wxWidgets is NOT code ...
Credits: 919
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Issues with the code:
1) you are using #defines instead of consts
2) you didn't give us the errors
3) you have a function defined as type "void void"
4) I don't see (could be blind) where you are using your function pointer.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-27-2007, 11:19 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

You've got the whole idea about function pointers wrong.

void * isn't a function pointer, it's simply just a pointer to an unknown type. Function pointers is much more complicated, if we can say it in that way.

When working with function pointers you need to know three things. First of all you need to know the return-type of the function, you need to point to. Second, you need to know how many parameters the function, you're trying to point at accepts. And third, you need to know exactly which of the types the parameter list accepts.

If we says that the function "write", has a return-type of an integer, and accepts two parameters, one type of a character "cCharacter", and the second of the type integer "iTimes". The prototype would look like this:
Code:
int foo(char, int);
Now you know how your function pointer should look like. A return-type as integer, one parameter as character, and the last parameter as an integer. This leads us to the syntax of function pointers, which looks a bit different than normal pointers. Here's how our function pointer should look like, to match our function:
Code:
int (*fpFunction)(char, int);
Now we can use fpFunction almost like a normal pointer. So now we need to point at the function, which matches the pointers need, and we can use fpFunction as we good use foo:
Code:
fpFunction = foo;
Now we're running. We can now use fpFunction, as I said, like the function foo. So we're gonna make a call:
Code:
fpFunction('a', 123); // One character, one integer.
This was what you need to know about function pointers, to make your program/project running. The syntax can be hard to understand in the beginning, but turns out to be fine, when you've used it for a while.

This is probably the best tutorial on the internet about function pointers, if you want to read more about them:
The Function Pointer Tutorials - Index
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-27-2007, 11:24 AM
sowmi sowmi is offline
Newbie
 
Join Date: Jun 2007
Posts: 5
Credits: 0
Rep Power: 0
sowmi is on a distinguished road
Default

Forgot to include my errors.
Below are my errors while compiling...

Errors
******
1)In PFUNCPTR pfun declration,
parse error before `;'
ANSI C++ forbids declaration of `pFun' with no type

2)In the array decalration, 3 errors
parse error before `;'
parse error before `;'
parse error before `;'

3)In (0 != SearchCommand...)
ElementLocalCommandHandler.cpp:1659: implicit declaration of function `int SearchCommand (...)'

4)After that when it was assigned to command,
ElementLocalCommandHandler.cpp:1662: parse error before `.'

5)In the search function,

6)`pFun' undeclared (first use this function)
(Each undeclared identifier is reported only once
for each function it appears in.)

7)In method `int FunctionEntry::SearchCommand (char *)':
cannot convert `FunctionEntry' to `const char *' for argument `2' to `strcmp (const char *, const char *)'
cannot convert `FunctionEntry *' to `int' in return


I am just a learner and trying to do the same..May be the process I handled could be wrong.Could you please correct the code .....

Here are my answers & doubts
**********************
1)why cant I use #define and why it should be "const"
2)Attached the errors.
3)How should it be.
Because my reqt is,
int aa(int a);
void bb(char a,char b);
void cc(void);

Can I implement these different signatures with function pointers???
4)I am using the function pointer in my main function "Execute".


thanks,
Sowmi

Thanks in advance,
Sowmi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-27-2007, 11:32 AM
sowmi sowmi is offline
Newbie
 
Join Date: Jun 2007
Posts: 5
Credits: 0
Rep Power: 0
sowmi is on a distinguished road
Default

since my file contains other part of implementation I have given the snippet of my code alone..so, while compiling i got the above metnioned errors.

If possible can you correct the errors and make changes in my code itself.??

Thanks in advance,
Sowmi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 06-27-2007, 11:35 AM
sowmi sowmi is offline
Newbie
 
Join Date: Jun 2007
Posts: 5
Credits: 0
Rep Power: 0
sowmi is on a distinguished road
Default

So function pointer should match with the signatures that we define??..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-27-2007, 11:39 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

The function pointer needs to match with the function you're going to point to. That's exactly how normal pointers works too. You can't make a pointer, int * and then point it to a character. They have to fit each other.

Read my last post, or visit the link I provided, to get all the answers you need. You're saying that the code snippets only is a part of an implementation, so I don't know if you've got the function pointers correct in some other part of the code - but in the snippets you provided, there were no valid function pointers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-27-2007, 11:42 AM
sowmi sowmi is offline
Newbie
 
Join Date: Jun 2007
Posts: 5
Credits: 0
Rep Power: 0
sowmi is on a distinguished road
Default

Thanks...

Just now I was looking into your example and also going thro' the website ...

I think I need to learn the concepts well...
Ok...Will try once I get the complete information about the fucntion pointers.

Sowmi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-28-2007, 11:11 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,535
Last Blog:
wxWidgets is NOT code ...
Credits: 919
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Quote:
Originally Posted by sowmi View Post
Here are my answers & doubts
**********************
1)why cant I use #define and why it should be "const"



thanks,
Sowmi

Thanks in advance,
Sowmi
#Define is extremely dangerous. What it does is an indiscriminate substitution in your source code. Where this causes a problem is things like the following:
Code:
#define pi 3.1415

int Compiler;
the variable name will become Com3.1415ler, which is not a valid variable name.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-29-2007, 03:29 AM
Nilecoder713 Nilecoder713 is offline
Newbie
 
Join Date: Jul 2007
Posts: 1
Credits: 0
Rep Power: 0
Nilecoder713 is on a distinguished road
Default Not sure what you mean

Quote:
Originally Posted by WingedPanther View Post
#Define is extremely dangerous. What it does is an indiscriminate substitution in your source code. Where this causes a problem is things like the following:
Code:
#define pi 3.1415

int Compiler;
the variable name will become Com3.1415ler, which is not a valid variable name.
Hello WingedPanther. I am new here and I was browsing the forums and I notice what you said above, and I think you made a mistake.

You are right in what you say about "#define"', but the above example would never happen.

The "pi" in "Compiler" would not be replaced with 3.14515. That is a variable declaration, and therefore should not be changed. The variable Compiler does have the letters "pi" in it, but it's a completely different variable name.

The preprocessor would only copy and paste itself when it encounters "pi", and no where else.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue writing to file: pointer to a class which contains pointers to other classes Sheemer C and C++ 0 08-21-2007 01:17 AM
C basics. justin1993 C and C++ 4 07-24-2007 07:56 AM
What do pointers do? Sionofdarkness C and C++ 15 06-15-2007 07:43 PM


All times are GMT -5. The time now is 08:59 AM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1083.85
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 903.18
John ........ 902.37
Brandon W ........ 789.89
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 240.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads