Reputation: 325
I want to copy my define into my var IP:
#define IP_ADDR {169, 254, 0, 3}
struct
{
// ....
char IP[4];
} COM_INFO;
memcpy(COM_INFO.IP, IP_ADDR, 4);
But it doesn't work.
Upvotes: 2
Views: 164
Reputation: 8861
Your define
must be like this:
#define IP_ADDR ((unsigned char []){169, 254, 0, 3})
Now you can use memcpy
on it.
Example Code
#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;
}
Example Output
169.254.0.3
Upvotes: 2
Reputation: 2483
IP_ADDR
will be pasted wherever its referenced (by the pre-processor). So, you could do something like the following:
int main(int argc, const char* argv[])
{
// Initialize the COM_INFO structure.
COM_INFO comInfo = {
// ...
IP_ADDR, // {169, 254, 0, 3} will be pasted here
// ...
};
return 0;
}
Upvotes: 1