473,398 Members | 2,404 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,398 software developers and data experts.

unions

sorry to post so soon after having posted before. I'm using a union
and I've read that a union can only hold a value of one member at a
time. The following function uses a union, but i don't know whether
the errors I'm getting is because of that, or because i have declared
the union instances wrong, can you please help.
struct crecord {
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};

struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];

};
struct drecord {
char customercode[5];
};

int loop = 200;
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords unionarray;

void determinestruct( union Allrecords unionarray, fstream& validdata,
char* temp2 )
{
union Allrecords *str_ptr1, *str_ptr2;

str_ptr2 = str_ptr1 + 1;

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
}

if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
{
str_ptr1 = str_ptr1.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
{
str_ptr1 = str_ptr1.Newdrecord.customercode, '\0';
}

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
{
str_ptr2 = str_ptr2.Newcrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
{
str_ptr2 = str_ptr2.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
{
str_ptr2 = str_ptr2.Newdrecord.customercode, '\0';
}

}
Jul 22 '05 #1
2 1594
muser wrote:
I'm using a union
and I've read that a union can only hold a value of one member at a
time.
There is no reason whatsoever for a programmer not working on hardware
interface code to use unions. Stop using them, and your problem will go
away.
The following function uses a union, but i don't know whether
the errors I'm getting is because of that, or because i have declared
the union instances wrong, can you please help.
After you remove the union, report the error message you get to the mailing
list.
struct crecord {
char customercode[5];
Read /Accelerated C++/, and use std::string instead of character arrays for
strings.
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};

struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];

};
struct drecord {
char customercode[5];
};

int loop = 200;
Don't put a variable up here, away from where it's used.
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;

};
union Allrecords unionarray;
In C++, structs classes and unions occupy the same namespace as function
names and such. You don't need to repeat 'union' here.
void determinestruct( union Allrecords unionarray, fstream& validdata,
char* temp2 )
That 'fstream' should be an 'istream'. Only the calling code cares if the
stream is a file.
{
union Allrecords *str_ptr1, *str_ptr2;

str_ptr2 = str_ptr1 + 1;

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
Use toupper() so you only need to check for 'C'.
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
That line does not do what you think it does.
}

if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)


While reading a binary file from a hard drive could be construed as
"interfacing hardware", you should instead read the code itself as a byte,
and then create the kind of record you need from a switch statement. Then
pass the istream into a method in each record that then fills in its values.

Consider writing a text file with delimiters, such as \t between fields and
\n between records. Then the record type appears in the first column, and
each record knows how to read the subsequent columns.

There is almost no reason to write a binary file format just to store data.

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
Jul 22 '05 #2
muser wrote:
sorry to post so soon after having posted before. I'm using a union
and I've read that a union can only hold a value of one member at a
time. The following function uses a union, but i don't know whether
the errors I'm getting is because of that, or because i have declared
the union instances wrong, can you please help.

This code has ALOT of problems, many not related to the use of union.

struct crecord {
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;

};

struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];

};
struct drecord {
char customercode[5];
};

int loop = 200;
union Allrecords{
struct crecord Newcrecord;
the "struct" keyword here is not needed.
struct irrecord Newirrecord;
or here struct drecord Newdrecord;
and here.

};
union Allrecords unionarray;
the union keyword is not needed and "unionarray" does not seem to be used.

void determinestruct( union Allrecords unionarray, fstream& validdata,
char* temp2 )
{
union Allrecords *str_ptr1, *str_ptr2;
again - no union keyword needed.

str_ptr2 = str_ptr1 + 1;
I don't know what you think the statement above is supposed to do but it
makes no sense. str_ptr1 is uninitialized so if you add 1 to it, you're
still uninitialized.

below you seem to call "peek" many times, why ? A switch statement
might be better.

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
{
str_ptr1 = str_ptr1.Newcrecord.customercode, '\0';
I have absolutly no idea what you're trying to do here. This is using
the "," operator and you're trying to reference a pointer as a
struct/union. (you probably mean 'str_ptr1->').
}

if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
This if condition is repeated below, why ?
{
str_ptr1 = str_ptr1.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
This if condition is also repeated below ?
{
str_ptr1 = str_ptr1.Newdrecord.customercode, '\0';
}

if(validdata.peek(temp2[0]) == 'c' || validdata.peek(temp2[0]) ==
'C')
This if condition is repeated above ?
{
str_ptr2 = str_ptr2.Newcrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'i' || validdata.peek(temp2[0]) == 'I'
|| validdata.peek(temp2[0]) == 'r' || validdata.peek(temp2[0]) == 'R'
)
{
str_ptr2 = str_ptr2.Newirrecord.customercode, '\0';
}
if(validdata.peek(temp2[0]) == 'd' || validdata.peek(temp2[0]) ==
'D')
{
str_ptr2 = str_ptr2.Newdrecord.customercode, '\0';
}

}


OK - I have no idea what you think this is supposed to do but I know
it's not going to do anything useful.

Try simplifying the code and repost it.

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
8
by: SteveM | last post by:
The general consensus I am getting is that nobody really uses unions much (engineers here at work) but this is an academic exercise for me so I am looking for an answer (I know there may be better...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
4
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
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
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:
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.