将数据传递给struct时出错

时间:2013-11-19 20:00:19

标签: c

我在函数内部的结构调用上遇到错误。在代码中,我将两个字节的数据转换为一个16位数据,我想将它存储到我的一个结构变量中。但是,我收到的错误是我无法查明的。编译器告诉我错误在行unsigned int fat.sector_size = combinedBytes;中。我在这做错了什么?提前致谢!

编辑:我得到的错误是

main.c:62:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token

main.c:62:19: error: expected expression before ‘.’ token`


struct fileSystem_info{
    unsigned int sector_size; //Sector size
    int cluster_size_in_sectors; //Cluster size in sectors
    int entries_in_root_directory; //Number of entries in root directory
    int sectors_per_fat; //Sectors per file allocation table
    int reserved_sectors; //Number of reserved sectors on the disk
    int hidden_sectors; //Number of hidden sectors on the disk
    int sector_number_of_first_copy_of_fat; //Sector number of the first copy of the file allocation table
    int sector_number_of_first_sector_of_root_directory; //Sector number of the first sector of the root directory
    int sector_numner_of_first_sector_of_first_usable_data_cluster; //Sector number of the  first sector of the first usable data cluster
};

//Converts two 8 bit data to one 16 bit data
unsigned converter(unsigned mostSignificant_bit, unsigned leastSignificant_bit){
    uint16_t value = (uint16_t)(mostSignificant_bit << 8) | leastSignificant_bit;
    //return((mostSignificant_bit * 256) + leastSignificant_bit);
    return (value);
}

unsigned int sectorSize (){
    struct fileSystem_info fat;
    unsigned char first_byte = buffer[11];
    printf("%hhu \n", first_byte);
    unsigned char second_byte = buffer[12];
    printf("%hhu \n", second_byte);
    unsigned int combinedBytes = converter ((int)second_byte, (int)first_byte);
    unsigned int fat.sector_size = combinedBytes;
    return (combinedBytes);
}

2 个答案:

答案 0 :(得分:1)

下面

unsigned int fat.sector_size = combinedBytes;

删除类型

fat.sector_size = combinedBytes;

(......也许休息一下......; - )


在下面的评论中提及版本信息的问题:

做这样的事情:

#define VERSION_MAJOR (0)  /* \                                          */
#define VERSION_MINOR (1)  /* +--<-- adjust those for upcoming releases. */
#define VERSION_MICRO (42) /* /                                          */

struct fileSystem_info
{
  unsigned int version;
  unsigned int sector_size; //Sector size

  ...

初始化struct fileSystem_info的实例时,请执行:

struct fileSystem_info fsi = {0};
fsi.version = (VERSION_MAJOR << 24) | (VERSION_MINOR << 16) | VERSION_MICRO};

这样做可以使您的最大版本号为255.255.65535

由于您总是将version写入磁盘,以后可以通过从结构中读取第一个unsigned int version来确定您编写的版本,然后决定如何继续阅读。这可能与结构以及如何在程序的不同版本中更改version后解释其内容相关。

答案 1 :(得分:1)

标识符不能包含.

目前

 unsigned int fat.sector_size = combinedBytes;

您正在定义unsigned int类型的新变量,称为fat.sector_size(这是一个无效的标识符。

如果要引用sector_size变量的成员fat,请不要使用变量定义的语法。只需写下

fat.sector_size = combinedBytes;