CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: pointers

  1. #1
    Join Date
    Feb 2002
    Location
    Trinidad
    Posts
    37

    pointers



    this might seem trivial to some of u but i'm jus unsure:
    i need to use a pointer to a function
    void suspend_resume()
    i wanted to use a long pointer but the compiler only lets me define the pointer as
    void (*lp)()=suspend_resume;
    my question is wat data type would this pointer be(long,int)?
    and how do i make the pointer global so i could use it in another function in my project?



  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: pointers

    Its type is void (*)(). In C++, that means "pointer to a function taking no arguments and returning void". In C it means "pointer to a function with no argument information and returning void".

    As for making global: the same as any other - for preference, don't. If you must, define it in a .cpp/.c file and declare it extern in a .h file.

    He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: pointers

    The type is void (*)() but it is clearer sometimes to use typedef, thus:


    typedef (*pfnVoid_t)();




    Then you can call this type pfnVoid_t whenever you want to use it.

    pfnVoid_t myFunction = suspend_resume;

    You can also typedef it without the * as follows:


    typedef void (fnVoid_t)();



    then you need to use a pointer when you want to assign to a real function: thus

    fnVoid_t *myFunction = suspend_resume;




    This means less ugly code but it also means another programmer has to go off and look up somewhere what your typedef means, whereas at least with the "ugly" notation one can see immediately what it means.

    I prefer to use these typedefs only scoped, either in a class or a namespace.

    As for making your function pointer global, it might be better instead to externally define a function that returns your function pointer. This will allow it to be externally accessed but not modified.

    Thus:

    // in .h file
    extern pfnVoid GetFunction();
    // in C++ file that owns the function
    pfnVoid GetFunction() { return myFnPtr; }
    // in other C++ file
    GetFunction()(); // second set of brackets to execute the function




    (Of course use more meaningful descriptor than GetFunction() )


    The best things come to those who rate

  4. #4
    Join Date
    Feb 2002
    Location
    Trinidad
    Posts
    37

    Re: pointers

    thanx
    but a more meaningful descriptor?explain pls


  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: pointers

    I mean that the name of your get function should give some description as to what the function it is returning does.

    Someone who sees a line of code that looks like this:


    GetFunction()();



    will immediately as "what does that line actually do?" Ok, they will see that it retrieves a function pointer and executes it, but what is this function pointer?



    The best things come to those who rate

  6. #6
    Join Date
    Feb 2002
    Location
    Trinidad
    Posts
    37

    Re: pointers

    hey
    when i try to use
    pfnVoid_t myFunction = suspend_resume;

    i keep getting errors
    'suspend_resume not being declared in this scope'
    and 'anci c++ forbids declaration of pfnVoid_t without a type'
    any suggestions


  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: pointers

    you need to tell me what you are doing where.

    If suspend_resume is a class member function then the prototype won't work.

    I assume you did:

    typedef void (*pfnVoid_t)();





    The best things come to those who rate

  8. #8
    Join Date
    Feb 2002
    Location
    Trinidad
    Posts
    37

    Re: pointers

    ok
    i dont have any classes- at least not yeti'm more accustomed to c-programing and even my experience in that isnt vast
    so basically suspend_resume() is a fn in the same source file
    i'm using a timer to interrupt the running of a process and suspend_resume() is supposed to be the timer proc callback fn
    thats y i need to get a pointer to this fn, it should be an argument in the SetTimer() fn
    hope i'm clear


  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: pointers

    This is the same problem as your other one then.

    You can write Windows programs in C too - using Win32 SDK, although it's worth learning C++. A lot of the things you need function pointers for in C are replaced with virtual functions, which are much simpler.

    I think you probably want a multi-threaded program, but you haven't said anywhere what your two "processes" are.


    The best things come to those who rate

  10. #10
    Join Date
    Feb 2002
    Location
    Trinidad
    Posts
    37

    Re: pointers



    my two processes are jus simple programs i write my self
    reading a file and outputting something
    calculating some averages
    jus a couple simple programs where each would run for more than a few seconds so i can interrupt it in a time that the user could appreciate wats goin on
    i'm really stumped
    my major problem here is a timing mechanism to stop the process after say bout 2-3 seconds
    then i need to suspend the program so i could resume it
    if i use the SetTimer() and i have the timer callback as NULL would control be passed back to the main() fn where i started the process? or is the event jus sent and the process continues to run?
    i'm not sure the WaitFor...() fn would accomplish what i want
    not from the reading i did anyways
    if i'm wrong of course tell me!
    pls
    any help u could offer


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured