English 中文(简体)
C. 结 论 材料包
原标题:C Copy char to struct
  • 时间:2011-11-21 14:46:40
  •  标签:
  • c

我读到了一组投入(传感器),每颗传感器可得到0(起飞)或1(接)。 我拿着这些数值,可以把所有传感器都考虑在内。 1 每一传感器的比照。

我不想在我的法典中使用这些价值观,但我并不认为,这是好的想法,而这一结果又产生了另一个char子,其中一 set子对《守则》非常感兴趣,因为当时《守则》被.弄。

反之,我是想像这样作一个方向:

struct sensors {
    unsigned int Sensor0:1;
    unsigned int Sensor1:1;
    unsigned int Sensor2:1;
    unsigned int Sensor3:1;
    unsigned int Sensor4:1;
    unsigned int Sensor5:1;
    unsigned int Sensor6:1;
    unsigned int Sensor7:1;
}  

struct sensors s1;
memcpy(buf, (char*)&sensors, 1);

但是,从我宣读的“结构”中,也许不能在相互记忆之后节省每个组成部分,并且可能添加dding子和其他 st子,使这不再发生。

我错了吗? 是否有更好的办法这样做?

最佳回答

在Blagovest的答复中加入:

实地办法似乎良好。 然而,你不得不指示编辑不要在田间进行婚礼。 对海合会而言,在结构定义之后插入>attribute__(包装)

struct sensors {
    unsigned char Sensor0:1;
    unsigned char Sensor1:1;
    unsigned char Sensor2:1;
    unsigned char Sensor3:1;
    unsigned char Sensor4:1;
    unsigned char Sensor5:1;
    unsigned char Sensor6:1;
    unsigned char Sensor7:1;
} __attribute__ ((packed)); 

Note that GCC before 4.4 used to introduce padding for char fields irrespective of this directive; see the documentation on warning option -Wpacked-bitfield-compat for more information.

问题回答

根据您目前对<条码>传感器的定义,汇编者将添加条码,因为<条码>int有一致性要求,通常在<条码>(int)边界上。 此外,<代码>int 范围必须大于<条码>,因此可以容纳超过8条旗帜,你认为不需要。

如果你宣布这样做(使用<条码>,代之以<<>unsign char>/code>,则不应进行婚礼,因为<条码><>/代码>的校对调和要求最小:

struct sensors {
    unsigned char Sensor0:1;
    unsigned char Sensor1:1;
    unsigned char Sensor2:1;
    unsigned char Sensor3:1;
    unsigned char Sensor4:1;
    unsigned char Sensor5:1;
    unsigned char Sensor6:1;
    unsigned char Sensor7:1;
}

这不仅可能在一个非常奇怪的平台上开展工作,在座标上<>CHAR_BIT !=8。

如果你想要超安全,那么你可以而且具有一环的特性。 也许你希望用一个阵列,而不是一个障碍来做到这一点。 你们可以把它归结为一只冰圈子,而它根本就不起作用。

然而,任何电灯C的汇编者都将把未签名的电离层存储器放在相邻的记忆中,这样就应该能够安全地做像这样的某种复印件。 汇编者通常只有在有不同规模的类型时才能进行额外的dding。 遗憾的是,我不知道这是否会方便你工作,因为你有指挥器(buf,(char*)&传感器,1)将复制你首批感官数据,这不是你想要的。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签