Thread: How to execute function

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    How to execute function

    Hi, this question is suitable to micro controller programming. I have a function and fix its location at some particular address in linker file. Now i want to execute this function on particular condition. How to do it? I am thinking of using function pointers like
    Code:
    Void (*fctptr) (void).
    Can i assign an address to this fctptr same as the address of the function in linker file like fctptr = (void *)8000.
    Any help? This is the typical application of bootloader.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Given this
    void (*fctptr) (void);
    You call it with
    fctptr();

    > Can i assign an address to this fctptr same as the address of the function in linker file like fctptr = (void *)8000.
    Yeah, you should be able to.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    my confusion is if i a pointer is pointing to unsigned char like
    Code:
    unsigned char *p;
    then we type cast it to unsigned char and assign to p like for example
    Code:
    p = (unsigned char *)0x100;
    but in the case of function pointers how should i type cast like for exampe
    Code:
    int (*fptr)(int);
    and if i am trying to assign any address how should i typecast? can i do simply as below?
    Code:
    fptr = (void *)1000;
    For everything can i simply use (void *)?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A function pointer cast looks like this.
    Code:
    #include <stdio.h>
     
    int main ( ) {
      int (*fp)(void);
      fp = (int(*)(void))100;
      if ( fp() ) {
      }
      return 0;
    }
    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.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Now i Understand, Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gets() function doesn't execute
    By mohsen in forum C Programming
    Replies: 4
    Last Post: 07-12-2013, 02:43 PM
  2. How to execute a top priority function?
    By zenniz in forum C Programming
    Replies: 2
    Last Post: 03-14-2013, 06:25 AM
  3. How to execute a function with a certain probability
    By siddhabathula in forum C Programming
    Replies: 4
    Last Post: 09-10-2010, 07:56 PM
  4. Using the exec call function to execute an app
    By it01y2 in forum C Programming
    Replies: 5
    Last Post: 12-20-2009, 01:08 PM
  5. Passing a function name as a variable to execute it?
    By Zuul in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:42 PM

Tags for this Thread