Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2121278
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-07-08 15:39:00

1. error: variable-sized object may not be initialized
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ( int argc, char *argv[] )
  4. {
  5.     const int n = 5;
  6.     int a[n]={1,2,3,4,5};
  7.     return EXIT_SUCCESS;
  8. }
c规定数组大小为常量,变量n被修饰为只读变量,可惜再怎么修饰也不是常量
详细见: 
2. x ? : y   是什么鬼
  1. x ? : y 等同于 x ? x : y

3. 指针运算 
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main (int argc, char *argv[])
  4. {
  5.     int* ip1, *ip2, iv;
  6.     char* cp1, *cp2, cv;
  7.     ip1 = (int*)0x518;
  8.     ip2 = (int*)0x500;
  9.     iv = ip1 - ip2;
  10.     printf("iv=%d\n", iv);

  11.     cp1 = (char*)0x518;
  12.     cp2 = (char*)0x500;
  13.     cv = cp1 - cp2;
  14.     printf("cv=%d\n", cv);

  15.     return 0;
  16. }
这个例子出自《经典C面试真题精讲》.(李亚锋) P11 107指针运算
iv=24/4=6 cv=24/1=24
对于整数指针当它自加或自减时,实际上移动的是4个字节
4.结构体
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct __HEAD__
  5. {
  6.     char c;
  7.     int num;
  8. }__attribute__ ((__packed__)) HEAD;

  9. typedef struct __HEAD2__
  10. {
  11.     char c;
  12.     int num;
  13. }HEAD2;

  14. int main (int argc, char *argv[])
  15. {
  16.     printf("pack=%d,nopack=%d\n", sizeof(HEAD),sizeof(HEAD2));
  17.     return 0;
  18. }
pack=5,nopack=8




阅读(1044) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~