我有一个存储在unsigned int
中的函数地址和一个函数指针。
现在我希望function_pointer
包含function_address
unsigned int function_address = 0xdeadbeef;
void(*function_pointer)(void) = function_address; // not valid
如何在C中做到这一点?
答案 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;