C language advanced

Source: Internet
Author: User
Tags define function

Try to write down your 1.1 million drops-
1. cross-platform development
1) header file inclusion
Use the define switch to include different libraries and define function types
# Ifndef WIN32
# Include "socket_posix.h"
# Else/* WIN32 */
# Include "socket_win32.h"
# Endif /*! WIN32 */
The applicability of if should be indicated at the end.
 
2) replace functions
# Undef errno
# Define errno WSAGetLastError ()
# Define read (fd, buf, size) recv (fd, buf, size, 0)
 
2. Common macros
Today we see a very beautiful application:
Macro # is used to initialize IPv4 and IPv6 address information.

# 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 */
 


From a Mao pig
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.