473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Changing each value in a struct one by one

MrPickle
100 100+
Is there a way to go through a struct changing each value without writing it out manually, eg not doing:

myStruct.a = 5;
myStruct.b = 7;
myStruct.c = 9;

I have a struct containing 17 ints, I then read in the data from a file all at once. I then want to go through each int and convert it to little endian. I figured there's gotta be an easier way than converting all the values one by one.

I'm using C++
Dec 6 '08 #1
4 2118
Ganon11
3,652 Expert 2GB
If these ints are at all related to each other, you can have your struct contain an array of ints, and use array processing techniques to get all the data, convert it, etc.

If this is all your struct contains, however, you have done little more than make a typedef int mystruct[17]; statement.
Dec 6 '08 #2
MrPickle
100 100+
I need them to have names so I can identify them, which is why they're in the struct.
Dec 6 '08 #3
JosAH
11,448 Expert 8TB
@MrPickle
There are more ways to skin a cat; have a look:

Expand|Select|Wrap|Line Numbers
  1. struct _your_variables {
  2.    int a, b, c, d ... q;
  3. }
  4. struct _your_struct {
  5.    union  {
  6.       struct _your_variables var;
  7.       int v[17];
  8.    } vars;
  9.    /* other members here */
  10. }  
  11.  
or you can name them by using the preprocessor:

Expand|Select|Wrap|Line Numbers
  1. #define A vars[0]
  2. #define B vars[1]
  3.  ...
  4. #define Q vars[16]
  5.  
  6. int vars[17];
  7.  
And there are more.

kind regards,

Jos
Dec 6 '08 #4
Banfa
9,065 Expert Mod 8TB
Personally I would say the solution is to write a function that reads the correct number of bytes from the file (which is presumably big endian) and converts that to an integer in little endian format and returns it rather than calling whatever file reading routine you are currently calling.

If you actually read an array of bytes and then use bitwise operators (left shift and or) to construct the int then you can actually write a portable version of such a function that does not care what the endian of the current platform is.
Dec 6 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Amit Gaur | last post by:
Hi newbie to python here, I have a binary file and i need to change the endian format..little to big as well as vice versa..could anyone help me out. thanks ---------------------------------...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
1
by: blueturtle | last post by:
Hi, I've implemented a hook (WH_CALLWNDPRO), using the sample posted at MSDN magazine. ( see url: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/ ) I succeed in intercepting the...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.