representation of MAC address in C code

M

Mark

Hello,

I often see such representation of MAC address in C code, e.g.
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

__
Mark
 
J

James Kuyper

Hello,

I often see such representation of MAC address in C code, e.g.
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

It's also not possible to pass an array by value to a C function, or to
return it as the value of the function; both of those are allowed for
structs.
 
B

Ben Bacarisse

Mark said:
I often see such representation of MAC address in C code, e.g.
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

Ironically, you may be best placed to say since you often see this sort
of thing in C code -- I don't. What does the C code (in which you often
see these things) do that could not be done as simply with an array? If
nothing, then it's probably just for familiarity -- many people find C's
arrays surprising and the struct wrapper removes some of the oddities.
 
L

Les Cargill

Mark said:
Hello,

I often see such representation of MAC address in C code, e.g.
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

__
Mark


Because then you have a "struct mac_addr" - or preferably:

typedef struct {
unsigned char bytes[6];
} mac_addr;

so the intent of the object is clearer.
 
R

Roberto Waltman

Mark" said:
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

In addition to what was posted already, you gain some type safety.
A function expecting a MAC address as a char array (instead of a
mac_addr struct) may accept any char array as (a possibly invalid)
argument.
 
L

Lynn McGuire

Hello,

I often see such representation of MAC address in C code, e.g.
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

__
Mark

And the MAC address is moving from 48 bits
to 64 bits:
http://en.wikipedia.org/wiki/MAC_address

Lynn
 
J

Jorgen Grahn

Hello,

I often see such representation of MAC address in C code, e.g.
struct mac_addr {
unsigned char bytes[6];
}Why necessary put an array in a structure, why not just have an array? What
benefit does this provide, other that it allows to assign a struct (and in C
it'snot possible to assign an array)?

It's also not possible to pass an array by value to a C function, or to
return it as the value of the function; both of those are allowed for
structs.

Also (or put differently) you lose the '6' when you don't pass by
value. The callee must have its own knowledge of the array length,
and sizeof won't work.

I always make a point of wrapping fixed-size arrays in structs, so
they behave more normally. Oddly, some libraries we use at work
represents MACs and IPv6 addresses with typedefs like this:

typedef uint8_t Ipv6Address_t[16];

Don't know why, since there /are/ standard types for this already in
the OS we use, and those standard type use structs of course.

Anyway, we've had bugs caused by the typedef: it looks too much like
call by value when you say 'void foo(Ipv6Address_t addr);'.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top