Thread: Casting function pointers into char arrays and back

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    Casting function pointers into char arrays and back

    Hi all, first time poster here.

    I am not sure how "usefull" my question will end up being, but i've been wondering about the following:

    You can cast between datatypes, so i was wondering if it was possible to cast a function (using a function pointer) into an array. I am not really thinking about the use of this, just something that came to my mind. For example maybe cast it (the function pointer) into a char array and then itterate through each "block" (byte), displaying the binary representation of it.

    To this end i've written the following quick test:

    Code:
    #include <iostream>
    using namespace std;
    
    void foo(void) // test function
    {
    	cout<<"In foo()"<<endl;
    }
    
    void printBinary(const char bin) // print binary representation
    {
    	unsigned char dummy=1;
    
    	for(int i=7; i>=0; --i)
            {
    		if(bin & (dummy<<i))
    			cout<<"1";
    		else
    			cout<<"0";
    	}
    }
    
    int main()
    {
    	void (*fptr)(void);
    	fptr=foo;
    
    	char* a=(char*)fptr; // cast function pointer into array
    
    	cout<<"Size: "<<sizeof(a)<<endl; // will give me the size of the pointer and not the actual size of the function, how would you get the size of the function?
    	for(int i=0; i<sizeof(a); ++i) // will only print 4 times because sizeof(a) will yield only size of the pointer and not the function
            	printBinary(a[i]); // print binary representation of each block
    
           	cout<<endl;
    
    	return 0;
    }
    Now does this make sense? If not what are some of the mistakes i've made (both syntactically and understanding wise)? Also would it be possible to convert an array back into a function pointer (cast back the other way)?

    This is more or less a train of thought that is not really something related to an assignment or required material for my courses, just something that might help my general understand of the language.

    Oww yeah, i've just started programming in general for the first time about a year ago.

    Any clarification will be greatly appreciated!
    Last edited by coolman0stress; 07-04-2003 at 06:45 PM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by Salem
    [B]
    > Also would it be possible to convert an array back into a function pointer (cast back the other way)?
    Yeah, if you get the type of the function pointer, and the cast right.
    Not sure.
    J.5 Common Extensions
    The following extensions are widely used in many systems, but are not portable to all
    implementations.
    ...
    J.5.7 Function pointer casts
    1 A pointer to an object or to void may be cast to a pointer to a function, allowing data to
    be invoked as a function (6.5.4).
    2 A pointer to a function may be cast to a pointer to an object or to void, allowing a
    function to be inspected or modified (for example, by a debugger) (6.5.4).
    Code:
        fptr = (void(*))a();
    Which is legal C, but my C++ compiler (gcc) rejects it because 'a' is a non-function.
    Sure
    fptr = (void(*))a;

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    Great, thanks for the replies so far.

    As far as compiler goes, so far i've only tried using borland (bcc32). I not entirely sure how to display assembly, but i'll look into that matter.

    I'll try the C code you guys suggested "fptr = (void(*))a();" and see if that works or not and since i was trying to work in C++, would explain why i kept on getting errors with everything i tried.

    Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM