views:

351

answers:

2

How do I write the MAC address ff:ff:ff:ff:ff:ff as a char[] in C?

Do I just do char macaddress[6] = "%0xFF%0xFF%0xFF%0xFF%0xFF%0xFF";

I'm not sure. Thanks!

+1  A: 

I rather do like this char macaddress[] = "\xff\xff\xff\xff\xff\xff";

There is some coding guide lines for char array initializations, because need to be null-terminated and the size is actually 7.

Do not initialize an array of characters using a string literal with more characters (including the '\0') than the array. Therefore, it is necessary to specify the correct size of a string literal (char s[4] = "abc";).
However, because the result of the expectation always can be obtained even if the size of the string literal is changed, the method of not describing the size (char s[] = "abc";) is recommended.

ref: http://www.caravan.net/ec2plus/guide.html

S.Mark
Thanks for the link.
Jasie
+3  A: 
char macaddress[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Timbo
doesn't `char macaddress[6] = {0xff};` initialize all the items to `0xff`?
Matt Joiner
No, that initializes to { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 }.
Timbo