LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 04-19-2013, 05:27 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Sharing variables in C.


Hi:
I have variable chk which is type unsigned long. Let's suppose this variable occupies four bytes (if it does not that's another problem). The program transmits a file over a line byte by byte at the same time maintaining a checksum which it transmit when done transmitting the file. The problem is I must transmit chk one byte at a time. How do I get access to the four bytes making chk? If I could declare

unsigned long chk;
unsigned char byte0 (at the same location as chk);
unsigned char byte1
unsigned char byte2
unsigned char byte3


then byte0 would be the least significant byte of chk. Also, I could do

lsbyte= chk mod 256 with the same result but I do not want to do arithmetic. Can two variables be declared so that they share the same location?
 
Old 04-19-2013, 05:41 AM   #2
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
There are two ways:

One is pointers: make 4 pointers, one for each byte.
byte0 = address of chk+(0 * sizeof char);
byte1 = address of chk+(1 * sizeof char);
byte2 = address of chk+(2 * sizeof char);
byte3 = address of chk+(3 * sizeof char);

I think that one is the most elegant;
On the other hand, you can also use the values by shifting around chk.
 
Old 04-19-2013, 05:49 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by stf92 View Post
The problem is I must transmit chk one byte at a time.
Why?

Quote:
Originally Posted by stf92 View Post
How do I get access to the four bytes making chk?
How about
Code:
unsigned long chk;
unsigned char* bytes = &chk;
printf("%d\n", bytes[0]);
...

Quote:
Originally Posted by stf92 View Post
then byte0 would be the least significant byte of chk.
Actually, that depends on the system endianness. When sending data over network, you have to allways define your transmission protocol so that the code works regardless of the byte order of both machines.

Quote:
Originally Posted by stf92 View Post
Also, I could do
lsbyte= chk mod 256 with the same result but I do not want to do arithmetic.
You have to do arithmetic, be it an int arithmetic or pointer arithmetic.

Quote:
Originally Posted by stf92 View Post
Can two variables be declared so that they share the same location?
Yes, that's what pointers are for. See above.
 
Old 04-19-2013, 11:48 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You can isolate bytes within a multibyte word with 'and's and 'shift's:
Code:
unsigned long word;  // assume 4-byte word, per your spec.
unsigned int  chksum;

   word = 0x12345678;

   chksum =    word & 0x000000ff;
   chksum += ( word & 0x0000ff00 ) >>  8;
   chksum += ( word & 0x00ff0000 ) >> 16;
   chksum += ( word & 0xff000000 ) >> 24;
--- rod.
 
Old 04-19-2013, 12:44 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thank you guys, I now see there are quite a few solutions. Still another one, I think, is unions. For instance:

Code:
union checksum {
unsigned long chk;
unsigned char bytes[4];
}
Here checksum.chk and checksum.bytes share the same memory location.
 
Old 04-19-2013, 01:19 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The use of unions as a form of 'conversion' memory space will only work in this case if the order of the bytes in unimportant (such as the way I've computed a simple sum checksum in the prior article). In a more 'CRC-like' scenario, where shifts and X-ORs are incorporated, the algorithm is sensitive to the byte ordering. The C language does not specify byte order of multibyte words, so the code would be unportable.

--- rod.
 
Old 04-19-2013, 11:54 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Here is the program, in case anyone cares.
Attached Files
File Type: txt setuid_05.txt (4.7 KB, 32 views)
 
Old 04-20-2013, 10:59 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by stf92 View Post
Hi:
I have variable chk which is type unsigned long. Let's suppose this variable occupies four bytes (if it does not that's another problem). The program transmits a file over a line byte by byte at the same time maintaining a checksum which it transmit when done transmitting the file. The problem is I must transmit chk one byte at a time. How do I get access to the four bytes making chk? If I could declare

unsigned long chk;
unsigned char byte0 (at the same location as chk);
unsigned char byte1
unsigned char byte2
unsigned char byte3


then byte0 would be the least significant byte of chk. Also, I could do

lsbyte= chk mod 256 with the same result but I do not want to do arithmetic. Can two variables be declared so that they share the same location?
Quote:
I have variable chk which is type unsigned long. Let's suppose this variable occupies four bytes (if it does not that's another problem)
- you have to read https://en.wikipedia.org/wiki/Inttypes.h#inttypes.h - nowadays fixed number of bits data types are readily available.


Quote:
The problem is I must transmit chk one byte at a time. How do I get access to the four bytes making chk?
- no problem:

Code:
chk & (((uint32)0xffu) << 0u)
chk & (((uint32)0xffu) << 1u)
chk & (((uint32)0xffu) << 2u)
chk & (((uint32)0xffu) << 3u)
is portable.

Last edited by Sergei Steshenko; 04-20-2013 at 11:02 AM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
Two variables sharing memory? Ephracis Programming 2 04-01-2005 12:18 AM
C - sharing variables between forked processes? ocularbob Programming 5 03-14-2004 03:29 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration