473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigning values to a struct

Hi,

I'm in the process of porting some code from a 3rd party and have hit
a problem with the following:

typedef struct {
unsigned char Red;
unsigned char Green;
unsigned char Blue;
}TBoxColour;

//---------------------------------------------------------------------------
void DoSomething(voi d)
{
TBoxColour Col;

Col = (TBoxColour){10 , 20, 30};
}

My compiler (CodeGear C++ Builder) complains with the error:

Improper use of typedef 'TBoxColour'

It seems a perfectly logical was of assigning values to the struct
members, but is it legal, or was the original programmer just
exploiting some quirk in a specific compiler (don't know which)?

Thanks

Aug 28 '07 #1
17 18749
On Aug 29, 12:59 am, "Alf P. Steinbach" <al...@start.no wrote:
Try

BoxColor const color = { 10, 20, 30 };

Cheers, & hth.,

- Alf
Hi Alf,

Sorry, I should have said, a single instance of this TBoxColour gets
assigned new values in 100's of locations throughout the code.

I suppose the only way is:

Col.Red = 10;
Col.Green = 20;
Col.Blue = 30;

The original method looks more elegant, This is just ugly, but more
importantly it's not easy for me to change the code with a simple
search and replace.

Aug 29 '07 #2
Cliff wrote:
On Aug 29, 12:59 am, "Alf P. Steinbach" <al...@start.no wrote:
>Try

BoxColor const color = { 10, 20, 30 };

Cheers, & hth.,

- Alf

Hi Alf,

Sorry, I should have said, a single instance of this TBoxColour gets
assigned new values in 100's of locations throughout the code.

I suppose the only way is:

Col.Red = 10;
Col.Green = 20;
Col.Blue = 30;

The original method looks more elegant, This is just ugly, but more
importantly it's not easy for me to change the code with a simple
search and replace.
You can define a function (a pseudo-constructor):

BoxColor createBoxColor( int a, int b, int c) {
BoxColor bc = { a, b, c };
return bc;
}

and then use it anywhere you need to assign:

Col = createBoxColor( 10, 20, 30);

(I understand that I barge in without reading the rest of the thread,
sorry for that; if what I wrote is bogus, forgive me and ignore it)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 29 '07 #3

Cliff <us******@btcon nect.comwrote in message...
On Aug 29, 12:59 am, "Alf P. Steinbach" <al...@start.no wrote:
Try
BoxColor const color = { 10, 20, 30 };
Cheers, & hth., Alf

Hi Alf,

Sorry, I should have said, a single instance of this TBoxColour gets
assigned new values in 100's of locations throughout the code.
I suppose the only way is:

Col.Red = 10;
Col.Green = 20;
Col.Blue = 30;

The original method looks more elegant, This is just ugly, but more
importantly it's not easy for me to change the code with a simple
search and replace.
Well, since you won't say, maybe this will drag it out of you:
struct BoxColor{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
BoxColor() : Red(0),Green(0) ,Blue(0){}
BoxColor( unsigned char R, unsigned char G,
unsigned char B ) : Red(R), Green(G), Blue(B){}
void Color( unsigned char R, unsigned char G,
unsigned char B ){
Red = R; Green = G; Blue = B;
return;
}
};

// if you cannot change this struct, see BoxColor2
typedef struct{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
}TBoxColour;

struct BoxColor2 : TBoxColour{
BoxColor2(){ Red = 0; Green = 0; Blue = 0; }
BoxColor2( unsigned char R, unsigned char G,
unsigned char B ){
Red = R;
Green = G;
Blue = B;
}
};
int main(){
BoxColor bc1( 10, 20, 30 );
cout<<"bc1 Red="<<int(bc1. Red)<<" Green="
<<int(bc1.Green )<<" Blue="<<int(bc1 .Blue)<<std::en dl;
BoxColor bc2;
cout<<"bc2 Red="<<int(bc2. Red)<<" Green="
<<int(bc2.Green )<<" Blue="<<int(bc2 .Blue)<<std::en dl;
bc2.Color( 40, 50, 60 );
cout<<"bc2 Red="<<int(bc2. Red)<<" Green="
<<int(bc2.Green )<<" Blue="<<int(bc2 .Blue)<<std::en dl;

BoxColor2 BC2( 10, 20, 30 );
TBoxColour TBC;
TBC = BC2;
cout<<"TBC Red="<<int(TBC. Red)<<" Green="
<<int(TBC.Green )<<" Blue="<<int(TBC .Blue)<<std::en dl;

TBC = BoxColor2( 30, 40, 50 );
cout<<"TBC Red="<<int(TBC. Red)<<" Green="
<<int(TBC.Green )<<" Blue="<<int(TBC .Blue)<<std::en dl;
// ------------------------------------
return 0;
} // main()

That, or use Victor's example.

--
Bob R
POVrookie
Aug 30 '07 #4
On Aug 30, 3:03 am, "BobR" <removeBadB...@ worldnet.att.ne twrote:
Well, since you won't say, maybe this will drag it out of you:
Sorry, I'm not intentionally withholding anything, what else would you
like to know?

I'll summarise what I am trying to do:

I have some c++ code that I am porting to a different compiler.

The original programmer has used a non standard way of assigning
values to members of several types of struct.

For example:

Col = (TBoxColour){10 , 20, 30};

My compiler doesn't want to know this, so I have to alter every
occurrence (many 100's) to a legal alternative. So I'm looking for a
solution that most closely resembles this format so that I can
automatically make the changes with a search and replace. This will
significantly reduce the probability of my making a typo, giving
problems later.

I am free to change the stuct declarations, or even change them to
classes.

Both yours and Victors solutions look viable and I thank you for
taking the time to help me out.

Cliff

Aug 30 '07 #5

Cliff <us******@btcon nect.comwrote in message...
On Aug 30, 3:03 am, "BobR" wrote:
Well, since you won't say, maybe this will drag it out of you:

Sorry, I'm not intentionally withholding anything, what else would you
like to know?
[snip]
I am free to change the stuct declarations, or even change them to
classes.
That's what I was after. I think adding constructor(s) and maybe a member
function (called 'method' by some) to the modified struct will set you on
the right path.

I didn't show it, but, you can get away with one constructor if you give it
default values:

// class BoxColor{ public: // same functionality as next line.
struct BoxColor{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
// BoxColor() : Red(0),Green(0) ,Blue(0){}

BoxColor( unsigned char R = 0, unsigned char G = 0,
unsigned char B = 0 ) : Red(R), Green(G), Blue(B){} // Ctor

void Color( unsigned char R, unsigned char G,
unsigned char B ){
Red = R; Green = G; Blue = B;
return;
} // Color(uchar,uch ar,uchar)
}; // struct BoxColor

Now the compiler will take that constructor as a default (one without
parameters), and you can use it with parameters too.

BoxColor ColorArray[200]; // all set to 0,0,0

std::vector<Box ColorvColors( 200, BoxColor( 1, 2, 3 ) );
// all set to 1,2,3

// cout<<int(vColo rs.at(199).Gree n); // output (last element): 2
vColors.at(3).C olor( 25, 77, 42 );
// cout<<int(vColo rs.at(3).Green) ; // output (forth element): 77

BoxColor Col; // or: BoxColor Col( 20, 40, 255 );
Col.Color( 25, 77, 42 );
// ....
Col.Color( 2, 99, 0 );
// ....
>
Both yours and Victors solutions look viable and I thank you for
taking the time to help me out.
Cliff
Glad to help. Let us know which solution you go with, and if you need
further assistance.

--
Bob R
POVrookie
Aug 30 '07 #6
On Thu, 30 Aug 2007 04:32:24 -0700, Cliff <us******@btcon nect.comwrote:
Col = (TBoxColour){10 , 20, 30};

My compiler doesn't want to know this, so I have to alter every
occurrence (many 100's) to a legal alternative. So I'm looking for a
solution that most closely resembles this format so that I can
automatically make the changes with a search and replace. This will
significantly reduce the probability of my making a typo, giving
problems later.
In that situation I'd go for a class, with an interface which is as
restricted as possible. No default constructor, const members,
"explicit", things like that -- if possible.

I wouldn't worry too much about typos; they are likely to be caught by
the compiler.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Aug 30 '07 #7
Hi,
myself Dilip
Replace following code with your code to get correct output.
typedef struct
{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
}TBoxColour;

void DoSomething(voi d)
{
struct typedef TBoxColour Col;
Col = (TBoxColour){10 , 20, 30};
}
Thanks,
Dilip Kumar
http://www.intelcs.com/IT-Companies/

Aug 31 '07 #8
Dilip

Thanks for your idea, however what you have suggested does not
compile.
The error is "{ expected" pointing to the "typedef" in the DoSomething
function.

Cliff

Aug 31 '07 #9

Cliff <us******@btcon nect.comwrote in message...
Dilip
Thanks for your idea, however what you have suggested does not
compile.
The error is "{ expected" pointing to the "typedef" in the DoSomething
function.
Cliff
But Cliff, (s)he said, "Replace following code with **your** code to get
correct output.".

In other words, do nothing!
void DoSomething(voi d){ // it MUST be 'C' code, '(void)' !C++
struct typedef TBoxColour Col;
Col = (TBoxColour){10 , 20, 30};
}
'DoSomething()' (if it compiled) does NOTHING! <G>
Did you solve your original problem?

--
Bob R
POVrookie
Aug 31 '07 #10

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

Similar topics

3
8268
by: Ben | last post by:
Hi all, This may sound easy but I'm having trouble assigning values to array element. My problem is as follows: m = for o in m: # Here first o is 'Peter'.. I want to do something like this: Peter = 10
6
7808
by: Pushkar Pradhan | last post by:
I have a struct like this: struct point { int x; int y; }; Then I need to assign a certain point "=" another pt. eg. struct point p1, pArr;
14
74810
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0; myarray=0; myarray=1; myarray=8;
9
3121
by: Chris Hiler | last post by:
Why isn't it possible to assign a value to a struct at run time? In my header file I have something like this: typedef struct SomeStruct_t { int Member1; int Member2; int Member3; };
5
1647
by: Manuel Graune | last post by:
Hello, while trying to learn how to program using objects in python (up to now simple scripts were sufficient for my needs) I stumbled over the a problem while assigning values to an object. The following piece of code shows what I intend to do: <---snip--->
0
7694
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...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7666
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...
1
5504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.