我想将我的定义复制到我的 var IP 中:
#define IP_ADDR {169, 254, 0, 3}
struct
{
// ....
char IP[4];
} COM_INFO;
memcpy(COM_INFO.IP, IP_ADDR, 4);
但是这不起作用。
最佳答案
您的定义
必须如下所示:
#define IP_ADDR ((unsigned char []){169, 254, 0, 3})
现在您可以对其使用memcpy
。
示例代码
#include <stdio.h>
#include <string.h>
#define IP_ADDR ((unsigned char []){169, 254, 0, 3})
int main(void)
{
unsigned char ip[4];
memcpy(ip, IP_ADDR, 4);
printf("%u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
return 0;
}
示例输出
169.254.0.3
关于c - 如何从定义初始化 char[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709872/