LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-10-2011, 11:55 PM   #1
Gerwar
LQ Newbie
 
Registered: Dec 2010
Location: Cancun
Distribution: Slackware 13.37-x86_64
Posts: 27

Rep: Reputation: 0
what do these lines wrote in C might mean?


in this section code i would like to know what does the

if (++mac->byte[5]) return;

stands for (only the ++mac->byte[5]).


typedef struct {
unsigned char byte[6];
} mac_t;

void
mc_mac_next (mac_t *mac)
{
if (++mac->byte[5]) return;
if (++mac->byte[4]) return;
if (++mac->byte[3]) return;
if (++mac->byte[2]) return;
if (++mac->byte[1]) return;
if (++mac->byte[0]) return;
mac->byte[5] = 1;
}
 
Old 03-11-2011, 08:40 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by Gerwar View Post
Code:
typedef struct {
        unsigned char byte[6];
} mac_t;

void
mc_mac_next (mac_t *mac)
{
        if (++mac->byte[5]) return;
        if (++mac->byte[4]) return;
        if (++mac->byte[3]) return;
        if (++mac->byte[2]) return;
        if (++mac->byte[1]) return;
        if (++mac->byte[0]) return;
        mac->byte[5] = 1;
}
First of all, mac is a pointer to a structure of mac_t type, so the mac->byte[index] refers to the index'th byte in the structure.

++thing is a preincrement operator. It increments thing by one first, before any evaluation is done.

In C, all nonzero integers evaluate to TRUE, so the if clauses test if the argument is nonzero after the increment. Because the type is unsigned char, the if clauses test if the byte increment did not wrap around (from 255 to zero).

In all, you can read the entire function like this:
  1. Increase byte[5] (the sixth byte) in the parameter structure. If it did not wrap around, return.
  2. Increase byte[4] (the fifth byte) in the parameter structure. If it did not wrap around, return.
  3. Increase byte[3] (the fourth byte) in the parameter structure. If it did not wrap around, return.
  4. Increase byte[2] (the third byte) in the parameter structure. If it did not wrap around, return.
  5. Increase byte[1] (the second byte) in the parameter structure. If it did not wrap around, return.
  6. Increase byte[0] (the first byte) in the parameter structure. If it did not wrap around, return.
  7. (Note: at this point the six bytes must be zero, because each wrapped around to zero.)
    Set byte[5] (the sixth byte) to 1.

As it happens, the function treats mac->byte as a 48-bit unsigned integer, using big-endian byte order (most significant byte first), and increments it by one. If it becomes zero, it sets it to one.

Hope this helps.
 
Old 03-12-2011, 12:22 PM   #3
Gerwar
LQ Newbie
 
Registered: Dec 2010
Location: Cancun
Distribution: Slackware 13.37-x86_64
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Nominal Animal View Post
First of all, mac is a pointer to a structure of mac_t type, so the mac->byte[index] refers to the index'th byte in the structure.

++thing is a preincrement operator. It increments thing by one first, before any evaluation is done.

In C, all nonzero integers evaluate to TRUE, so the if clauses test if the argument is nonzero after the increment. Because the type is unsigned char, the if clauses test if the byte increment did not wrap around (from 255 to zero).

In all, you can read the entire function like this:
  1. Increase byte[5] (the sixth byte) in the parameter structure. If it did not wrap around, return.
  2. Increase byte[4] (the fifth byte) in the parameter structure. If it did not wrap around, return.
  3. Increase byte[3] (the fourth byte) in the parameter structure. If it did not wrap around, return.
  4. Increase byte[2] (the third byte) in the parameter structure. If it did not wrap around, return.
  5. Increase byte[1] (the second byte) in the parameter structure. If it did not wrap around, return.
  6. Increase byte[0] (the first byte) in the parameter structure. If it did not wrap around, return.
  7. (Note: at this point the six bytes must be zero, because each wrapped around to zero.)
    Set byte[5] (the sixth byte) to 1.

As it happens, the function treats mac->byte as a 48-bit unsigned integer, using big-endian byte order (most significant byte first), and increments it by one. If it becomes zero, it sets it to one.

Hope this helps.
Thank you very much sir, now i know what the ++thing does. It was only necessary to tell me what did the ++thing but, the general explanation gave me a better understanding. It was a really good explanation by the way.
 
  


Reply

Tags
pointers, programming language


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-26-2009 11:29 PM
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM


All times are GMT -5. The time now is 06:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration