谁能解释一下为什么这段代码无法编译?
main.c
typedef struct ext2_group_desc
{
unsigned long bg_block_bitmap; /* Blocks bitmap block */
unsigned long bg_inode_bitmap; /* Inodes bitmap block */
unsigned long bg_inode_table; /* Inodes table block */
unsigned int bg_free_blocks_count; /* Free blocks count */
unsigned int bg_free_inodes_count; /* Free inodes count */
unsigned int bg_used_dirs_count; /* Directories count */
unsigned int bg_pad;
unsigned long bg_reserved[3];
} group_desc;
int main() {
char buf[1024];
group_desc gd;
gd = (group_desc) buf;
return(0);
}
终端
$ bcc -ansi -c test.c
test.c:7.26: error: need scalar or pointer or void
test.c:7.26: error: assignment to/from struct/union of a different type
$
最佳答案
您缺少 ext2_group_desc
的一些定义(可能在某些包含的文件中?)。我猜它是一些struct
。然后你可能会编码
gd = *(ext2_group_desc*) buf;
如果要将 buf
处的内存复制到 gd
结构中。
请阅读一本有关 C 编程的好书。学习 C 需要很多天的工作。
同时启用编译器中的所有警告。
关于从c中的字符缓冲区中转换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13738108/