//struct BBB{
// long a;
// char c1;
// char c2;
// long b;
// long c;
//}*p;//sizeof(struct BBB)=16;
//int main()
//{
// p = (struct BBB*)0x100000;
// printf("0x%x", p + 0x1);//加整個結構體大小為0x10010
// printf(" 0x%x",(unsigned long) p + 0x1);//整型加1,0x100001
// printf(" 0x%x", (unsigned long*)p + 0x1);//加sizeof(unsigned long)*1,為0x100004
// printf(" 0x%x",(char *) p + 0x1);//加sizeof(char)*1,為0x100001
// system("pause");
// return 0;
//}
2.結構體中結構體,共用體中結構體的大小
//union AAA{
// struct {
// char c1;
// short sl;
// char c2;
// }half;
// short kk;
//}number;
//struct BBB{
// char ucFirst;
// short us;
// char c2;
// short uo;
//}half;
//struct tagCCC
//{
// struct
// {
// char c1;
// short sl;
// char c2;
// }half;
// long kk;
//};//結構體是個整體按4對齊6+4=10,short和long中最大所占字節為4,4的整數倍為12
//int main()
//{
// printf("%d %d %d\n", sizeof(union AAA), sizeof(struct BBB), sizeof(struct tagCCC));
// system("pause");
// return 0;
//}
執行結果: 1字節對齊:4 6 8 4字節對齊:6 8 12 3.如果case語句中沒有break,那麼它之後的語句都會被執行。 4.指針和const的聲明: (1)指針所指對象是可讀的 const int *p; int const *p; (2)指針是可讀的 int *const p; (3)指針和指針所指對象都是可讀的 const int * const p; int const *constp; 5.動態開辟
void GetMemory(char *p,int len)
{
p=(char *)malloc(len);
}
int main()
{
char *p;
GetMemory(p,10);
strcpy(p,"bit");
printf("%s",p);
free(p);
}
上述代碼有三處錯誤!!! a.此時調用函數p動態開辟和主函數p所指不是同一個地方,因為p只是臨時拷貝,沒傳地址過去。 b.strcpy,出現錯誤,此時p無指向。 c.free時並未將其開辟(臨時變量)的釋放,且應在free後讓指針指向空。 正確代碼為:
void GetMemory(char **p,int len)
{
*p=(char *)malloc(len);
}
int main()
{
char *p;
GetMemory(&p,10);
strcpy(p,"bit");
printf("%s",p);
free(p);
p=NULL;
}