# c中基础函数用法 ## `setsockopt`函数 第二个参数level是被设置的选项的级别,如果想要在套接字级别上设置选项,就必须把level设置为 SOL_SOCKET。 ```c++ int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); ``` ## `memset` 函数 > void *memset(void *s, int ch, size_t n); ### 作用 > En > Copies the value static_cast``(ch) into each of the first count characters of the object pointed to by dest. If the object is a potentially-overlapping subobject or is not TriviallyCopyable (e.g., scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined. If count is greater than the size of the object pointed to by dest, the behavior is undefined. > Ch > 将ch的值复制到dest指向的对象的前count个字符中。如果对象是潜在重叠的子对象或不是TriviallyCopyable(例如,标量,C兼容的结构或平凡可复制的类型的数组),则行为是未定义的。如果count大于dest指向的对象的大小,则行为是未定义的。 复制ch的值到 s 指向的对象的前 n 个字符中。 ### 语法 ```c++ void *memset(void *s, int ch, size_t n); ``` ### 参数 - s: 指向要填充的内存块的指针 - ch: 要被设置的值 - n: 要被设置的内存块的大小,以字节为单位 ### 返回值 - 返回一个指向内存块 s 的指针。 ## `snprintf` 函数 ### 作用 > En > Writes formatted data from variable argument list to sized buffer. > Ch > 将格式化的数据从变量参数列表写入大小缓冲区。 ### 语法 ```c++ int snprintf( char *buffer, size_t buf_size, const char *format, ... );//可变参数 ``` ### 参数 - buffer: 指向用于存储输出结果的缓冲区的指针。 - buf_size: 缓冲区的最大长度。 - format: 格式字符串,包含要写入缓冲区的文本。它可以包含以下占位符: - %s: 字符串 - %c: 单个字符 - %d: 十进制整数 - %f: 浮点数 - %x: 十六进制整数 ## `memcpy` 函数 ### 作用 > En > Copies count bytes from the object pointed to by src to the object pointed to by dest. > Both objects are reinterpreted as arrays of unsigned char. > If the objects overlap, the behavior is undefined. > If either dest or src is a potentially-overlapping subobject > (e.g., a field in a C-compatible struct) or is not TriviallyCopyable > (e.g., scalar, C-compatible struct, or an array thereof), > the behavior is undefined. If count is zero, the function does nothing. > Ch > 将src指向的对象的count个字节复制到dest指向的对象。 > 两个对象都被重新解释为无符号字符数组。 > 如果对象重叠,则行为是未定义的。 > 如果dest或src是潜在重叠的子对象(例如,C兼容结构中的字段) > 或不是TriviallyCopyable(例如,标量,C兼容结构或其数组), > 则行为是未定义的。如果count为零,则函数不执行任何操作。 ### 语法 ```c++ void *memcpy(void *dest, const void *src, size_t count); ``` ### 参数 - dest: 指向目标对象的指针 - src: 指向源对象的指针 - count: 要被复制的字节数 ### 实现 ```c++ void *memcpy(void *dest, const void *src, size_t count) { char *d = dest; const char *s = src; while (count--) *d++ = *s++; return dest; } ```