Is it possible to copy function to a const function pointer by memcpy?

This is a discussion on Is it possible to copy function to a const function pointer by memcpy? within the C Programming forums, part of the General Programming Boards category; I want to change an CONST function pointer after it's initialed, I tried the following, i printed the pointer address ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Is it possible to copy function to a const function pointer by memcpy?

    I want to change an CONST function pointer after it's initialed, I tried the following, i printed the pointer address copyed by memcpy, the address is different from the function address. Also, I get a seg fault on executing it. How can i do this?

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    void function(void){
    }

    const void (*function_pointer)(void)=NULL;

    int main(){
    memcpy(&function_pointer, &function, sizeof(fuction_pointer));
    fprintf(stderr, "%p, %p\n", &function_pointer, &function);
    function_pointer();
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    &function_pointer is not the same as the content of function_pointer.

    memcpy() should complain that you are trying to modify a const variable, really, so it should not allow you to do what you want to do. The variable is either const, or it isn't! If you modify it, then it should not be const.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    it is not const pointer - it is pointer to const - should be no problem to modify it

    Code:
    #include <stdio.h>
    
    void function(void)
    {
    	printf("Called\n");
    }
    
    const void (*function_pointer)(void)=NULL;
    
    int main()
    {
    	function_pointer = &function;
    	function_pointer();
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21