#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
//错误试列
void getMemory(char *p, int n)
{
//p为局部变量,传进来参数的值
p = (char *)malloc(n);
}
//错误试列
void getMemory(char **p, int n)
{
//传入二级指针,改变指针的指向
*p = (char *)malloc(n);
}
//位域结构体
struct bit
{
int a:2; int b:4; int c:2;
};
int main()
{
//试题1:
char *p = NULL;
getMemory(&p, 20);
strcpy(p, "hello world");
free(p);
p =NULL;
bit j;
char *c = (char *)&j;
*c = 0x99;
//(c)10(b)0110(a)01
cout << j.a << ' ' << j.b << ' ' << j.c << endl;
return 0;
}
GetMemory与位域
于 2012-10-25 09:43:32 首次发布