1. C中可变参数函数作为函数参数:

void media_debug_set_handler(struct media_device *media, void (*debug_handler)(void *, ...), void *debug_priv)
调用:
media_debug_set_handler(media, (void (*)(void *, ...))fprintf, stdout);

2.可变参数函数

gstcaps.c

GstCaps *gst_caps_new_full (GstStructure * struct1, ...)
{
GstCaps *caps;
va_list var_args; va_start (var_args, struct1);
caps = gst_caps_new_full_valist (struct1, var_args);
va_end (var_args); return caps;
} GstCaps * gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
{
GstCaps *caps; caps = gst_caps_new_empty (); while (structure) {
gst_caps_append_structure_unchecked (caps, structure);
structure = va_arg (var_args, GstStructure *);
} return caps;
}

3.offsetof实现

#include <stddef.h>中  #define offsetof(TYPE, MEMBER) ((int)&((TYPE *)0)->MEMBER)

疑问:为什么自己同样实现报将指针强制类型转换为int,使用头文件中的怎么没有呢 ?

4.C的精髓

#include <stdio.h>
#include <stdlib.h> void getArray(long *p) {
int i, j, n = ;
int *p1 = malloc(); for (i = ; i < ; i++) {
for (j = ; j < ; j++) {
p1[n++] = n;
}
} *p = (long)p1;
} void printArray(void **p) {
int i, j;
int (*p1)[] = (int (*)[])p;
//int **p1 = (int **)p; //oops,for different deference protocoal for (i = ; i < ; i++) {
for (j = ; j < ; j++) {
printf("[%d %d]", p1[i][j], *(*(p1+i)+j)); //array pointer also can use ** to deference
}
printf("\n");
}
} int main() {
int **p = NULL; getArray((long *)&p); //三级指针可以强制转换成一级指针进行传参,印证只有值传递 printArray((void **)p); free((void *)p); return ;
}

5.GNU扩展

int main() {
int i;
char a[] = {[ ... ] = }; for (i = ; i < ; i++) {
printf("a[%d] = %d\n", i, a[i]);
} return ;
}

6.errno

errno 是记录系统的最后一次错误代码,错误代码定义在Linux内核的errno-base.h中。errno是一个int型的值,在errno.h中定义。
当linux C api函数发生异常时, 一般会将errno变量(需include errno.h)赋一个整数值, 不同的值表示不同的含义,可以通过查看该
值推测出错的原因。

注意errno记录的是最后一次出错的错误代码,感兴趣的错误代码可能被最新的错误覆盖!

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h> void main()
{
printf("errno = %d\n", errno); // errno=0
// chmod 444 tmp.txt
int fd = open("tmp.txt", O_RDWR); //此时errno=13: Permission denied
if (fd < ) {
// no log.txt
fd = open("log.txt", O_RDWR);
printf("error: %s\n", strerror(errno)); //由于log.txt不存在,此处报的是No such file or directory
}
}

7. C中左右两边都可以的强制类型转换

#include <stdio.h>
#include <stdlib.h> void* get_mem(){
return malloc();
} void main()
{
float *p1;
int *p2, *p3;
p1 = (float*)get_mem();
printf("p1=%p\n", p1);
p3 = (int*)p1; //在右边进行强制类型转换
*(float**)&p2 = p1; //在左边进行强制类型转换
printf("p2=%p\n", p2);
printf("p3=%p\n", p3);
printf("Hello World\n");
free(p1);
}

&p2是一个常量,(float**)指定为地址,解引用为向此常量地址存储空间中写入值,此地址刚好就是p2指针的存储空间。

最新文章

  1. RDIFramework.NET ━ 9.12 表字段管理 ━ Web部分
  2. CheckBoxList 用法
  3. 进程kswapd0与events/0消耗大量CPU的问题
  4. Thread .join 的用法一例
  5. 基于Selenium2+Java的UI自动化(2) - 启动浏览器
  6. Array.prototype.map()详解
  7. 【随笔】Linux &amp; Shell &amp; Minecraft
  8. Jquery实现图片轮播源码
  9. Web模板引擎本质前奏
  10. 认识html文件基本结构
  11. 使用HTML5的页面资源预加载(Link prefetch)功能加速你的页面加载速度
  12. HDU Today(dijskra)
  13. hadoop安全模式
  14. HDU1051 Wooden Sticks 【贪婪】
  15. U31网管配置
  16. android脚步---UI界面修改,增加按钮和监听
  17. PHP正则式PCRE
  18. NoSuchMethodError解决方法
  19. Bootstrap switch 切换状态踩坑
  20. jQuery-2.DOM---创建节点及节点属性

热门文章

  1. PHP函数总结 (六)
  2. hdu6394Tree lct
  3. 51nod-1534-博弈
  4. css实现椭圆
  5. iOS系统版本与机型的对应关系
  6. 编译EXE文件的时候pcap编译不进去。 pyinstaller pcap pypcap 报错
  7. 破解电视盒 运营商送的,各种型号。通用 TTL 破解电视盒,更新华为悦盒
  8. flask+script命令行交互工具
  9. sql server的远程连接
  10. postgresql的copy