从c中的字符缓冲区中转换对象

标签 c casting

谁能解释一下为什么这段代码无法编译?

ma​​in.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/

相关文章:

C代码,写入没有零和奇数的整数

c++ - 代码:: block 和 MPI

c++ - 编译时类型检查 C++

c - 为什么以下两个函数中只有一个打印出正确的转换: text-file to 16 and 8-bit displays?

c++ - 目前在 Ubuntu C/C++ 中如何将 IANA 时区名称转换为 UTC 偏移量

c - 如何忽略一组输入中的负值

mysql - 在 node.js - mysql 中使用 where 子句选择查询?

sql - derby数据库中的转换问题

java - 为什么此转换不生成编译器警告?

c - 指针 - 转换为结构指针 + Malloc