• C

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:

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented: 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

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,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
>> 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 "". this is
usually stored in a string.

char *ip = "";
>>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 ("")
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 = "";
           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

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,
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

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

ip addresses are stored in unsigned integers(32 bits).
what 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 stands for ffffffffU in the uint32_t representation
Jaime OlivaresSoftware ArchitectCommented:
>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:

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));
skyblue01Author Commented:
Sorry for the late response.

I'll try what's mentioned in your comments.   Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.