473,395 Members | 1,624 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,395 software developers and data experts.

Inserting values in struc array

Hi guys,

I had some problem inserting values a structure array in C++.

I had define for example

[PHP]struct UNITS {
unsigned char VariableA;
unsigned char VariableB;
// the code continue until VariableN for example
};[/PHP]

How can I insert the value into the variables inside the UNITS automatically? For example, I wish to do the following code:

[PHP]
UNITS unitA;
unitA.VariableA = count;
unitA.VariableB = count + 1;
unitA.VariableC = count + 2;
unitA.VariableD = count + 3;[/PHP]

How could I implement the previous code in a loop so that it will automatically assigned an integer to each byte in unitA? One of the way that I had tried is given below, but it cannot compile as it gave an error saying I cannot convert '*_w64' to 'unsigned char*' for the second line in the following code ("unsigned char *ByteArray = &unitA;").

[PHP]
UNITS unitA;
unsigned char *ByteArray = &unitA;
int count;
for(count=0; count<100; count++)
{
*(ByteArray+count)=count;
}[/PHP]

Thanks a lot for your help.
Sep 30 '08 #1
9 2422
Banfa
9,065 Expert Mod 8TB
The best way would be the second chunk of code you posted (i.e. doing it long hand).

Your final chunk of code is definately a hack depending on all the variables being of type unsigned char, to get it working you could replace

unsigned char *ByteArray = &unitA;

with

unsigned char *ByteArray = &unitA.VariableA;

However as I said it would definately be a hack.

Is it not possible to use an array of unsigned char inside the structure? Then you could use a loop for initialisation without resorting to hacks that are a long way from best practice.
Sep 30 '08 #2
I could not use an array in my struct because those are only an example. I am trying to implement a struct which will have different types of variables in it, instead of just unsigned char.
Declaring it as

unsigned char *ByteArray = &unitA.VariableA;

would also cost a long code which I hope to reduce.

Could I get the address of the object 'unitA' and then add values one byte at a time into the address and subsequent address of 'unitA'? Lets say 'unitA' is declared on address 0x0350. How could I put the first byte of value in the address 0x0350, the next byte in the address 0x0351 and so on?
Oct 1 '08 #3
boxfish
469 Expert 256MB
One of the way that I had tried is given below, but it cannot compile as it gave an error saying I cannot convert '*_w64' to 'unsigned char*' for the second line in the following code ("unsigned char *ByteArray = &unitA;").
If you want to do this, then try using a reinterpret_cast to get rid of the error:
Expand|Select|Wrap|Line Numbers
  1. unsigned char *ByteArray = reinterpret_cast<unsigned char*>(&unitA);
Hope this helps.
Oct 1 '08 #4
Banfa
9,065 Expert Mod 8TB
Could I get the address of the object 'unitA' and then add values one byte at a time into the address and subsequent address of 'unitA'? Lets say 'unitA' is declared on address 0x0350. How could I put the first byte of value in the address 0x0350, the next byte in the address 0x0351 and so on?
Only if you knew and where able to precisely imitate the structure packing rules your platform uses.

What you are suggesting is a sure fire route to for strange and unpredictable problems later.

Tell us why you thing you want/need to do this.
Oct 1 '08 #5
I am receiving data from an external device. In this case, I will receive 76 bytes of data continuously, byte by byte, from an external microconverter. Each byte received represent different meaning, for example, first byte received represent the number of channels, the second byte represent price of that external device (just an example), therefore I wish to put the first byte in a variable named numOfChan, second byte in priceOfDevice, and so on. Hence I thought of using a struct to declare all the variables needed and then add byte by byte the data received into the variables in the struct.

[PHP] for(count=0; count<100; count++)
{
*(ByteArray+count)=functionToReceiveOneByte();
}[/PHP]

That was what I had previously actually. The functionToReceiveOneByte() had been tested and is working as intended, so the only problem now is to assigned those bytes received into suitable variables. If my method of solving this problem is wrong, is there any other methods that could achieve what I intend to do? Thanks in advance.

I had tried using the reinterpret_cast as suggested by boxfish and it manage to compile well. Thanks for that. But I need a few days before I could actually test on the hardware whether the code is doing as intended. Does that 'reinterpret_cast' return the address of the 'unitA' declared? I tried to read some examples from the web but I am quite confuse with what it does.
Oct 1 '08 #6
Hi guys,

I had tried using the code as suggested and most of the stuff turned out well and solved my major problems. I only need to do a few minor changes to the code before everything works completely fine. Thanks very much for your help guys. I really appreciate it. Thanks again.
Oct 9 '08 #7
Tassos Souris
152 100+
Note however that this works only if sizeof( unsigned char ) == 1.. you should consider this if you are concerned about portability..but for the thing you want it to it is fine!!! :-P :-P

Another solution would be to store in a static array the offsets of each of the member from the beginning of the struct... there is the offsetof macro in stddef.h file. Then, in the for loop you would retrieve these offsets from the array one by one sequentially to get the desired address... just an idea...
Oct 9 '08 #8
boxfish
469 Expert 256MB
I'm pretty sure the C++ standard guarantees chars to take up one byte.
Oct 9 '08 #9
Banfa
9,065 Expert Mod 8TB
It does and it guarantees access to all bytes in memory contiguously but it doesn't guarantee the number of bits in a byte.
Oct 9 '08 #10

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

Similar topics

10
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly...
9
by: Andrew Banks | last post by:
I'm running the following code in a C#.NET page and it doesn't enter the values into the DB. I'm certain the problem is to do with the txtBirth field. It allows users to enter a DOB as dd/mm/yyyy...
5
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
15
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
1
by: nasirmajor | last post by:
dear all, if anyone can help about inserting records into datagrid from arraylist. e.g i have a following sample,please anyone sujjest what can i change in it to make it better ArrayList...
8
by: Jim | last post by:
In a C# project I'm working on for an iterative design application, I need to dispose of a large arrray of a struct object and reinitialize the array between iterations. That is, the user starts...
0
by: srinivasaraonagisetty | last post by:
hi, I am faceing one problem, while inserting the data in db2 using clob. actually i am writing this type code: public class DBParam { private InputStream inputstream; private static int...
1
by: djmeltdown | last post by:
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky. I've tried the mysql_query statement without a...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.