C为函数指针赋值int

时间:2015-09-28 18:08:09

标签: c pointers integer

我有一个存储在unsigned int中的函数地址和一个函数指针。

现在我希望function_pointer包含function_address

的值
unsigned int function_address = 0xdeadbeef;
void(*function_pointer)(void) = function_address; // not valid

如何在C中做到这一点?

2 个答案:

答案 0 :(得分:7)

执行显式演员。

def dashes(number)
    string = number.to_s

    i = 0

    final = []

    while i < string.length

        digit = string[i].to_i

        if ((digit)%2 != 0) && (i==0) && (string.length == 1) && ((string.length - 1) == 0)             
            final.push(string[i])
        elsif ((digit)%2 != 0) && (i==0) && (string.length != 1) && ((string.length - 1) > 0)               
            final.push(string[i] + "-")
        elsif ((digit)%2 != 0) && (i>0) && (i!=(string.length - 1)) && ((string.length != 1))               
            final.push("-" + string[i] + "-")
        elsif ((digit)%2 != 0) && (i!=0) && (i==(string.length - 1))                
            final.push("-" + string[i])
        else 
            final.push(string[i])
        end

    i += 1

    end

   final_result = final.join('').gsub('--', '-').chomp('-')
   puts "final_result: #{final_result.inspect}"

   if final_result[0] == '-'
    final_result[1..-1]
   else
    final_result
   end
end

puts("Give me any number.")

answer = gets.strip

puts dashes(answer)

# > Give me any number.
# > 95601137
# > "9-5-60-1-1-3-7"

答案 1 :(得分:1)

试试这个

typedef void(*function_pointer_type)(void)
function_pointer_type *function_pointer = (function_pointer_type *) address;