程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> C語言個人進階

C語言個人進階

編輯:關於C

爭取把自己的一點一滴記下來-
1. 跨平台開發
1) 頭文件包含
通過define開關包含不同的庫,定義函數類型
#ifndef WIN32
#include "socket_posix.h"
#else  /* WIN32 */
#include "socket_win32.h"
#endif  /* !WIN32 */
結尾應該注明if適用情況。
 
2) 替換函數
#undef  errno
#define errno  WSAGetLastError()
#define read(fd, buf, size)  recv(fd, buf, size, 0)
 
2. 常用宏
今天看到非常漂亮的一個應用:
 利用宏##實現了IPv4和IPv6地址信息的初始化

#define FILL_SOCKADDR(AF_INET, sockaddr_in, sin, s,             \
                      host, port, count, addrlen, addrs)        \
  do                                                            \
    {                                                           \
      struct sockaddr_in *addr;                                 \
      int i;                                                    \
                                                                \
      addrlen = sizeof(struct sockaddr_in);                     \
                                                                \
      addr = (struct sockaddr_in *) calloc(count, addrlen);     \
      for (i = 0; i < count; ++i)                               \
        {                                                       \
          addr[i].sin##_family = AF_INET;                       \
          addr[i].sin##_port = port;                            \
          memcpy(&addr[i].sin##_addr.s##_addr,                  \
                 ADDR(host, i), host->h_length);                \
        }                                                       \
                                                                \
      addrs = (char *) addr;                                    \
    }                                                           \
  while (0)
 
#define fill_sockaddr(host, port, count, addrlen, addrs)        \
  FILL_SOCKADDR(AF_INET, sockaddr_in, sin, s,                   \
                host, port, count, addrlen, addrs)
 
#ifdef AF_INET6
#define fill_sockaddr6(host, port, count, addrlen, addrs)       \
  FILL_SOCKADDR(AF_INET6, sockaddr_in6, sin6, s6,               \
                host, port, count, addrlen, addrs)
#endif  /* AF_INET6 */
 


摘自 阿毛小豬
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved