• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

assigning values to different types

Hi, I'm new to C, and having trouble dealing with its types.   I have a structure with two variables:

struct A {
   unsigned char addr[6]; // MAC address
   uint32_t ip; // IP address
}

For MAC address, I want to assign the value: FF:00:FF:00:FF:00
For IP address, I want to assign the value: 255.255.255.255

How would I be able to do this?

I believe that I can't just do something like...

A->addr[0] = "\xFF";
A->addr[1] = "\x00";...

A->ip = 255255255255;

Could someone tell me how to handle types such as array of unsigned char and uint32_t?   Thanks.
0
skyblue01
Asked:
skyblue01
  • 3
  • 2
2 Solutions
 
van_dyCommented:
255.255.255.255 is the dotted decimal
notation for ip address. when stored in
an uint32_t datatype, the ip address
isnt stored as 255255255255, rather its
representation is 0xffffffffU on a 32
bit machine. one way to store the ipaddress
will be,

struct A {
   unsigned char addr[6]; // MAC address
   unsigned char ipaddr[4]; // IP address, store it like we stored the MAC
}data;

now we can have:FF:00:FF:00:FF:00
data.addr[0] = 0xff;
data.addr[1] = 0x00;
data.addr[2] = 0xff;
data.addr[3] = 0x00;
data.addr[4] = 0xff;
data.addr[5] = 0x00;

similarily for the ip:255255255255
data.ipaddr[0] = 255;
data.ipaddr[1] = 255;
data.ipaddr[2] = 255;
data.ipaddr[3] = 255;

printf("%x:%x:%x:%x:%x:%x\n", data.addr[0], data.addr[1], data.addr[2], data.addr[3], data.addr[4], data.addr[5]);
printf("%u.%u.%u.%u\n", data.ipaddr[0], data.ipaddr[1], data.ipaddr[2], data.ipaddr[3]);

hope this helps,
van_dy
0
 
skyblue01Author Commented:
van_dy, thanks for your reponse.

If I need to use uint32_t for ip address, I assume I have to do ipaddr = 0xffffffffU?   How does this conversion (255255255255 -> 0xffffffffU) work?

Also, for the mac addr, why do you attach '0x' before the value I want to assign?   Doesn't \x allow the compiler to take "FF" as a string literal?
0
 
van_dyCommented:
>> If I need to use uint32_t for ip address, I assume I have to do ipaddr = 0xffffffffU?
yes that is right. but usually the procedure that is followed to store ip address in uint32_t
is slightly different.

say you have an ip address in the dotted decimal representation, like "10.4.1.57". this is
usually stored in a string.

char *ip = "10.4.1.57";
>>How does this conversion (255255255255 -> 0xffffffffU) work?
Now in order to effect the above type of conversion, you can use the
function inet_aton(). please take a look into the manpage of inet_aton().
inet_aton() will convert the ascii representation of the ip address ("10.4.1.57")
to the appropriate representation in uint32_t datatype(actually in_addr_t datatype
which happens to be typedefed as  uint32_t).

as an example,

struct A {
   unsigned char addr[6]; // MAC address
   uint32_t ip; // IP address
} data;

struct in_addr xyz;

int main()
{
          char *myip = "10.4.1.57";
           inet_aton(myip, &xyz);
           // xyz.s_addr member contains the uint32_t representation of the ip
           //now you can do:
           data.ip = xyz.s_addr;

          // please take a look into the manpage of inet_aton() to learn about the various ways of
          // interconverting dotted decimal to unint32_t representations of ip address. you will also need
          // to look into struct  in_addr.

Regarding MAC:
A->addr[0] = "\xFF";
A->addr[1] = "\x00";

the above way of assigning to the addr[0] and addr[1] elements is incorrect.
reason being that the value of "\xff" is a pointer to this string, which totally
chnages the meaning of the assignment (what is assigned to addr[0] is the address
of the string "\xFF" not the value ff. another wat to do this is to use single quotes instead of double
quotes.

say:
A.addr[0] = '\xff';
A.addr[1] = '\x00';

'\xhh' (h being a hexadecimal number), is used to represent arbitrary bit patterns
for character types. so you can use these as well, instead of

data.addr[0] = 0xff;
data.addr[1] = 0x00;

as i specified in my first post.

hope this helps,
van_dy
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
van_dyCommented:
>> How does this conversion (255255255255 -> 0xffffffffU) work?

ip addresses are stored in unsigned integers(32 bits).
what 255.255.255.255 implies is, that the 32 bits be divided
in 4 fields, each of 8 bits wide. now interpret each of these
4 fields individually.
field1 = 255 = ff (in hex)
field2 = 255 = ff (in hex)
field3 = 255 = ff (in hex)
field4 = 255 = ff (in hex)

so the ip addresses 255.255.255.255 stands for ffffffffU in the uint32_t representation
0
 
Jaime OlivaresCommented:
>struct A {
>   unsigned char addr[6]; // MAC address
>   uint32_t ip; // IP address
>}
>For MAC address, I want to assign the value: FF:00:FF:00:FF:00
>For IP address, I want to assign the value: 255.255.255.255


If you have fixed values you can do this:

/* include this header to use memcpy */
#include "mem.h"

unsigned char def_mac[6] = { 0xFF, 0, 0xFF, 0, 0xFF, 0 };
unsigned char def_ip[4] = { 255, 255, 255, 255 };

struct A test;
memcpy(test.addr, def_mac, sizeof(test.addr));
memcpy(&test.mac, def_ip, sizeof(test.mac));
0
 
skyblue01Author Commented:
Sorry for the late response.

I'll try what's mentioned in your comments.   Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now