Thread: Help with function pointers

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Question Help with function pointers

    I'm having trouble trying to assess the data members. I tried using -> and . but the compiler keeps saying request for member `magic' in something not a structure or union, etc...

    I guess I'm having trouble understanding the concept of function pointers.

    Code:
     typedef struct _PCB {
       unsigned magic;
       unsigned used;
       unsigned short priority;
       unsigned short state;
       MEM_ADDR esp;
       PROCESS param_proc;
       void* param_data;
       PORT first_port;
       PROCESS next_blocked;
       PROCESS next;
       PROCESS prev;
      char* name;
    } PCB;
    typedef PCB* PROCESS;
    
    PORT create_process (void (*ptr_to_new_proc) (PROCESS, PARAM),
    		     int prio,
    		     PARAM param,
    		     char *name)
    {
       //CAN'T ACCESS THESE DATA MEMBERS
       ptr_to_new_proc->magic	= MAGIC_PCB;
       ptr_to_new_proc->used	= TRUE;
       ptr_to_new_proc->state	= STATE_READY;
       ptr_to_new_proc->priority	= prio;
       ptr_to_new_proc->first_port	= NULL;
       ptr_to_new_proc->name	= name;
       return NULL;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What the compiler says makes sense. You can't expect to be able to access members in a function pointer as if it were a structure. It's a function pointer!

    What are you trying to do? Are you trying to simply pass a pointer to a structure to that function and then modify the structure? If so, you might try this syntax:
    Code:
    PORT create_process (PROCESS ptr_to_new_proc,
    		     int prio,
    		     PARAM param,
    		     char *name)
    [edit] If you want to read up on function pointers, try the tutorial. [/edit]
    Last edited by dwks; 10-10-2006 at 02:04 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Because function pointers are meant to be called, not dereferenced.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    But when you call it, you dereference it, do you not?

    But you can't expect it to act like a pointer to a structure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM