473,770 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 Totalcreditlimi t;

};

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.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
'C')
{
str_ptr1 = str_ptr1.Newcre cord.customerco de, '\0';
}

if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
|| validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
)
{
str_ptr1 = str_ptr1.Newirr ecord.customerc ode, '\0';
}
if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
'D')
{
str_ptr1 = str_ptr1.Newdre cord.customerco de, '\0';
}

if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
'C')
{
str_ptr2 = str_ptr2.Newcre cord.customerco de, '\0';
}
if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
|| validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
)
{
str_ptr2 = str_ptr2.Newirr ecord.customerc ode, '\0';
}
if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
'D')
{
str_ptr2 = str_ptr2.Newdre cord.customerco de, '\0';
}

}
Jul 22 '05 #1
2 1613
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 Totalcreditlimi t;

};

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.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
'C')
Use toupper() so you only need to check for 'C'.
{
str_ptr1 = str_ptr1.Newcre cord.customerco de, '\0';
That line does not do what you think it does.
}

if(validdata.pe ek(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
"interfacin g 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 Totalcreditlimi t;

};

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.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
'C')
{
str_ptr1 = str_ptr1.Newcre cord.customerco de, '\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.pe ek(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.Newirr ecord.customerc ode, '\0';
}
if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
'D')
This if condition is also repeated below ?
{
str_ptr1 = str_ptr1.Newdre cord.customerco de, '\0';
}

if(validdata.pe ek(temp2[0]) == 'c' || validdata.peek( temp2[0]) ==
'C')
This if condition is repeated above ?
{
str_ptr2 = str_ptr2.Newcre cord.customerco de, '\0';
}
if(validdata.pe ek(temp2[0]) == 'i' || validdata.peek( temp2[0]) == 'I'
|| validdata.peek( temp2[0]) == 'r' || validdata.peek( temp2[0]) == 'R'
)
{
str_ptr2 = str_ptr2.Newirr ecord.customerc ode, '\0';
}
if(validdata.pe ek(temp2[0]) == 'd' || validdata.peek( temp2[0]) ==
'D')
{
str_ptr2 = str_ptr2.Newdre cord.customerco de, '\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
5283
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 thing? I guess there might be some additional overhead with CLASSES, but is that really an issue with today's computers?
8
1625
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 ways to do this ;-) ). What I need to know is how to write the function prototype and the function definition for a function that returns a union. For example: Here is a class for a double linked list in which variable data is going to be...
6
13385
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
3958
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 value is declared to a variable in the union, the existing value is overwritten even though the new value is declared to a different variable than that of the first value. Now I'm just wondering what the use of this is. I'm sure there are lots,...
23
2835
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 relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
4
1766
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
3372
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 sake of Unions simply because I wanted to see one in action. Granted: it makes it possible to save a few bytes of storage when you have something that can be a chicken or a rooster, but not both, and you're always going to know which it is. ...
26
1926
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 Unions" and "union {unsigned char u; ...} ". Here's a concrete example: #include <stdio.h> int main(void)
11
2022
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 unions . My questions are : 1. Is it dangerous to use unions ? Is it worth the trouble if I want to save memory ? Are they error prone ? 2. I read that it is not possible to access more than 1 member at any instant from a union. What does this...
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.