我将结构定义为:
typedef struct cr3_page_entry
{
uint64_t ignored : 2; // ignored
uint64_t pwt : 1; // page-level write-through; indirectly determines the memory type used to access the PML4 table during linear-address translation (see Section 4.9.2)
uint64_t pcd : 1; // page-level cache disable; indirectly determines the memory type used to access the PML4 table during linear-address translation (see Section 4.9.2)
uint64_t ignored2 : 7; // ignored
uint64_t address : 53; // rest is address up to MAXPHYADDR, then zeros
} cr3_page_entry_t;
这是一个 uint64_t,如果您查看它(64 位大小的结构)。现在,我有一个从中返回这样一个数字的汇编函数
extern uint64_t get_active_page();
但是,我不能直接转换为它:
cr3_page_entry_t entry = (cr3_page_entry_t) get_active_page();
因为我得到一个错误:
memory/paging.c:19:2: error: conversion to non-scalar type requested
cr3_page_entry_t entry = (cr3_page_entry_t) get_active_page();
这可以在不为某些指针转换创建另一个堆栈变量的情况下完成吗?
最佳答案
用uint64_t
和cr3_page_entry_t
结构定义union
。然后分配给uint64_t
成员。
typedef union _cr3_u
{
uint64_t i;
cr3_page_entry_t str;
} cr3_u;
cr3_u my_data;
my_data.i = get_active_page();
关于c - 为什么不能在 C 中直接将 number 转换为 struct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34060778/