1. error: variable-sized object may not be initialized
-
#include <stdio.h>
-
#include <stdlib.h>
-
int main ( int argc, char *argv[] )
-
{
-
const int n = 5;
-
int a[n]={1,2,3,4,5};
-
return EXIT_SUCCESS;
-
}
c规定数组大小为常量,变量n被修饰为只读变量,可惜再怎么修饰也不是常量
详细见:
2. x ? : y 是什么鬼
3. 指针运算
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
int main (int argc, char *argv[])
-
{
-
int* ip1, *ip2, iv;
-
char* cp1, *cp2, cv;
-
ip1 = (int*)0x518;
-
ip2 = (int*)0x500;
-
iv = ip1 - ip2;
-
printf("iv=%d\n", iv);
-
-
cp1 = (char*)0x518;
-
cp2 = (char*)0x500;
-
cv = cp1 - cp2;
-
printf("cv=%d\n", cv);
-
-
return 0;
-
}
这个例子出自《经典C面试真题精讲》.(李亚锋) P11 107指针运算
iv=24/4=6 cv=24/1=24
对于整数指针当它自加或自减时,实际上移动的是4个字节
4.结构体
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
typedef struct __HEAD__
-
{
-
char c;
-
int num;
-
}__attribute__ ((__packed__)) HEAD;
-
-
typedef struct __HEAD2__
-
{
-
char c;
-
int num;
-
}HEAD2;
-
-
int main (int argc, char *argv[])
-
{
-
printf("pack=%d,nopack=%d\n", sizeof(HEAD),sizeof(HEAD2));
-
return 0;
-
}
pack=5,nopack=8
阅读(1093) | 评论(0) | 转发(0) |