LinuxQuestions.org
Visit Jeremy's Blog.
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 02-24-2010, 01:23 PM   #1
shawley
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Rep: Reputation: 0
auto increment structures


I am converting some code from SCO unix to Linux. the SCO code increments a structure with ((STRUCT_DEF *)variable)++;

gcc on Linux gives a lvalue error. I am assuming and would like confirmation if possible, that the replacement for linux would be.
variable = (STRUCT_DEF *)(variable + sizeof(STRUCT_DEF));
is this correct?

And then there is this:

SCO code: (STRUCT_DEF *) variable +=3;
is this adding 3 or 3 X size of sturcture?

Linux replacement would be?
variable = ((STRUCT_DEF *)variable)+3;
or
variable = (STRUCT_DEF *)(variable + (sizeof(STRUCT_DEF) * 3);

Thanks for any help.

Scott Hawley
scott@shawley.net
 
Old 02-24-2010, 03:31 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by shawley View Post
I am converting some code from SCO unix to Linux. the SCO code increments a structure with ((STRUCT_DEF *)variable)++;

gcc on Linux gives a lvalue error. I am assuming and would like confirmation if possible, that the replacement for linux would be.
variable = (STRUCT_DEF *)(variable + sizeof(STRUCT_DEF));
is this correct?
Not if variable isn't a char pointer.
Code:
variable = (type-of-variable*)((char*)variable + sizeof(STRUCT_DEF));
Quote:
And then there is this:

SCO code: (STRUCT_DEF *) variable +=3;
is this adding 3 or 3 X size of sturcture?
If it's adding 3 bytes that is really bad code, so I would assume 3 X size of structure.


Quote:
Linux replacement would be?
variable = ((STRUCT_DEF *)variable)+3;
or
variable = (STRUCT_DEF *)(variable + (sizeof(STRUCT_DEF) * 3);
The second variation has the same problem that I mentioned above (i.e. that it would add 3*sizeof(STRUCT_DEF)*sizeof(*variable) bytes).

Quote:
Thanks for any help.

Scott Hawley
scott shawley net
I wouldn't put my email on a public web page if I were you.
 
Old 02-24-2010, 11:30 PM   #3
shawley
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks for the response and advice!
 
  


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
increment operator ++1 casperdaghost Linux - Software 8 02-24-2010 03:18 PM
SQL Auto Increment Error kdelover Programming 6 12-19-2009 07:55 PM
Auto increment an alphanumeric ID in C PMR Programming 3 05-22-2009 11:08 AM
how to auto increment 13 digit hexadecimal using script hchoonbeng Linux - Newbie 1 09-24-2008 11:03 PM
SQLite how to get last "AUTO INCREMENT" value blackman890 Programming 1 12-11-2006 01:22 PM

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

All times are GMT -5. The time now is 10:28 PM.

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