Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

quick C question - typedef u_char array and its pointer

Status
Not open for further replies.

tzushky

Member level 3
Joined
May 21, 2007
Messages
63
Helped
10
Reputation
20
Reaction score
0
Trophy points
1,286
Location
Sophia-Antipolis,France
Activity points
1,819
u_char in c

hello everyone

i have a small C manipulation problem.

u_char is the typedef of unsigned char. Used for bytes.
I define a MAC address as

typedef u_char mac_address[6];

and later in main() i have a pointer

mac_address *mac_src;


which will be assigned a value using a pointer to some packet data. This works just fine. My problem is printing the MAC to the screen in intelligible form ( xx:xx...xx)

Why? I try and get the values of the 6 components of the mac_src in memory by mac_src[0]...etc. This gets me their addresses, which on one side I understand...but how do I get to the contents of the addresses, my MAC components?? With *mac_src[0] ?

I had honestly thought I understood the idea of arrays and pointers in C, but it's making me nuts. PLZ help!

thanks in advance

Added after 59 minutes:

nobody curious yet?

i seem to have found a very lucrative way: printf( mac_src[0][0], mac_src[0][1]...)

because it seems that mac_src[0] is my pointer value and so inside it there is the table of bytes....weird...
 

typedef an array

mac_address is a pointer. And you declare a pointer to it. That makes your variable a pointer to a pointer. Try dereferencing it. I'm out of practice with C, but I think that is your problem.

typedef unsigned char mac_address[6];
mac_address *m;
mac_address d = {0xAE, 0x45,0xB3, 0x5A,0xE7, 0x83};
m = &d;

Now m is a pointer that points to an array. It is not an arry itself so you can't use indexing notation m[], you have to use pointer notation to access the various elements of d. The address of d is *m, the value stored at d is *d so it is also **m.
This is also the value stored at d[0]. To access the other elements use:
*(*m + 1) and so on. The compiler will use the number as the nth element of whatever value type is being accessed. In this case it does the equivalent of
*(*m + 1 * sizeof(uchar)) to calculate the address. (Actually a pentium will do it automatically with the appropriate address mode)

to format your mac address in hex 00:00:00:00:00:00 format use

char mybuffer[20];

sprintf( mybuffer, "%2X:%2X:%2X:%2X:%2X:%2X\n", **m, d[1], d[2], d[3], *(*m + 4), *(*m + 5)) ;

Added:
Now that I've thought about it a little (it's been awhile) since *m is the same as d in my example, then (*m)[5] is the same as d[5] so you can use indexing notation.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top