【发布时间】:2017-09-30 21:09:59
【问题描述】:
我想知道为什么在我的 malloc 之后所有修改都不起作用。 这是我用来说明这一点的代码:
#include <stdio.h>
#include <stdlib.h>
struct Age {
unsigned int age : 16;
unsigned int two : 2;
unsigned int notToBeInitialed: 2;
};
int init(struct Age * p){
p = (struct Age *) malloc( sizeof(struct Age) );
p->age = 5;
return 0;
}
int change(struct Age * p){
p->age = 99;
}
int getValue(struct Age * p){
return p->age;
}
int main(void) {
struct Age test;
init(&test);
printf( "Age.age : %d\n", getValue(&test) ); // gives me 0 , expected 5
change(&test);
printf( "Age.age : %d\n", getValue(&test) ); // gives me 99
return 0;
}
我做错了什么?
感谢您的帮助。
来源:http://www.tutorialspoint.com/cprogramming/c_bit_fields.htm 爱迪生:https://ideone.com/O59tqZ
【问题讨论】:
-
谢谢大家:你们都得到了+1,因为你们所有的答案都是真实的。我认为丹尼尔·特鲁格曼的回答更清楚。我可能把这个概念和另一种语言混为一谈了^^
标签: c field bit bit-fields