Connecting Tech Pros Worldwide Forums | Help | Site Map

struct problem

jesse
Guest
 
Posts: n/a
#1: Nov 14 '05
alo. i'm learning c at the moment, and i'm attempting to make little
experiments with struct types. here's my code:

#include <stdio.h>

struct bin {
char name[30];
int quantity;
float cost;
};

int main()
{
struct bin cablebin;

cablebin.name = "Printer Cables";
cablebin.quantity = 25;
cablebin.cost = 11.95;

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}

for some reason, i get the following error:
Error E2277 struct.c 13: Lvalue required in function main

line 13 is: cablebin.name = "Printer Cables";

i'm a bit confused; my code should be correct, so i don't know what's going.

jesse.



Martin Dickopp
Guest
 
Posts: n/a
#2: Nov 14 '05

re: struct problem


"jesse" <jengle@elementalsn.com> writes:
[color=blue]
> alo. i'm learning c at the moment, and i'm attempting to make little
> experiments with struct types. here's my code:
>
> #include <stdio.h>
>
> struct bin {
> char name[30];
> int quantity;
> float cost;
> };
>
> int main()
> {
> struct bin cablebin;
>
> cablebin.name = "Printer Cables";
> cablebin.quantity = 25;
> cablebin.cost = 11.95;
>
> printf("A bin of %d %s at $%f a piece will cost $%f.\n",
> cablebin.quantity, cablebin.name, cablebin.cost,
> cablebin.cost*cablebin.quantity);
>
> return 0;
> }
>
> for some reason, i get the following error:
> Error E2277 struct.c 13: Lvalue required in function main
>
> line 13 is: cablebin.name = "Printer Cables";
>
> i'm a bit confused; my code should be correct, so i don't know what's going.[/color]

You cannot assign to an array. Use

strcpy (cablebin.name, "Printer Cables");

instead and don't forget to include <string.h>.


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Peter Pichler
Guest
 
Posts: n/a
#3: Nov 14 '05

re: struct problem


"jesse" <jengle@elementalsn.com> wrote in message
news:l2PYb.23288$4Z4.21260@newssvr16.news.prodigy. com...[color=blue]
> alo. i'm learning c at the moment, and i'm attempting to make little
> experiments with struct types. here's my code:
>
> #include <stdio.h>
>
> struct bin {
> char name[30];
> int quantity;
> float cost;
> };
>
> int main()
> {
> struct bin cablebin;
>
> cablebin.name = "Printer Cables";
> cablebin.quantity = 25;
> cablebin.cost = 11.95;
>
> printf("A bin of %d %s at $%f a piece will cost $%f.\n",
> cablebin.quantity, cablebin.name, cablebin.cost,
> cablebin.cost*cablebin.quantity);
>
> return 0;
> }
>
> for some reason, i get the following error:
> Error E2277 struct.c 13: Lvalue required in function main
>
> line 13 is: cablebin.name = "Printer Cables";
>
> i'm a bit confused; my code should be correct, so i don't know what's[/color]
going.

C is not BASIC. Your cablebin.name is an array of char. If you want to fill
it, you must do it explicitly by strcpy or some such, a plain assignment
does not work.

There is a concession, but only for initializations, not assignments. You
could rewrite your program like this:

/* ...copy your declarations and stuff, they are OK. */

int main()
{
struct bin cablebin =
{
"Printer Cables",
25,
11.95
};

printf("A bin of %d %s at $%f a piece will cost $%f.\n",
cablebin.quantity, cablebin.name, cablebin.cost,
cablebin.cost*cablebin.quantity);

return 0;
}


Lewis Bowers
Guest
 
Posts: n/a
#4: Nov 14 '05

re: struct problem




jesse wrote:
[color=blue]
> alo. i'm learning c at the moment, and i'm attempting to make little
> experiments with struct types. here's my code:[/color]

Read faq: http://www.eskimo.com/~scs/C-faq/q8.3.html
[color=blue]
>
>
> #include <stdio.h>[/color]

#include <string.h>

[color=blue]
>
>
> struct bin {
> char name[30];
> int quantity;
> float cost;
> };
>
> int main()
> {
> struct bin cablebin;
>
> cablebin.name = "Printer Cables";
>[/color]

Replace the above with
strcpy(cablebin.name,"Printer Cables");

As an alternative you can initialize the struct during declaration like this:

struct bin cablebin = {"Printer Cables", 25, 11.95f};
thus eliminating the need to use assignments.
[color=blue]
> cablebin.quantity = 25;
> cablebin.cost = 11.95;
>
> printf("A bin of %d %s at $%f a piece will cost $%f.\n",
> cablebin.quantity, cablebin.name, cablebin.cost,
> cablebin.cost*cablebin.quantity);
>
> return 0;
> }
>
> for some reason, i get the following error:
> Error E2277 struct.c 13: Lvalue required in function main
>
> line 13 is: cablebin.name = "Printer Cables";
>
> i'm a bit confused; my code should be correct, so i don't know what's going.
>
> jesse.[/color]

Tim Hagan
Guest
 
Posts: n/a
#5: Nov 14 '05

re: struct problem


"jesse" <jengle@elementalsn.com> wrote in message
news:l2PYb.23288$4Z4.21260@newssvr16.news.prodigy. com...[color=blue]
> alo. i'm learning c at the moment, and i'm attempting to make little
> experiments with struct types. here's my code:
>
> #include <stdio.h>
>
> struct bin {
> char name[30];
> int quantity;
> float cost;
> };
>
> int main()
> {
> struct bin cablebin;
>
> cablebin.name = "Printer Cables";[/color]

You can't do that. It's a FAQ:

http://www.eskimo.com/~scs/C-faq/q8.3.html

--
Tim Hagan


E. Robert Tisdale
Guest
 
Posts: n/a
#6: Nov 14 '05

re: struct problem


jesse wrote:
[color=blue]
> I'm learning C at the moment and I'm attempting to [conduct] little
> experiments with struct types. Here's my code:
>
> #include <stdio.h>[/color]
#include <string.h>[color=blue]
>
> typedef struct bin {
> char name[30];
> int quantity;
> float cost;
> } bin;[/color]

inline
bin bin_create(const char* n, int q, float c) {
bin b;
b.name[29] = '\0';
strncpy(b.name, n, 29);
b.quantity = q;
b.cost = c;
return b;
}

inline const
char* bin_name(const bin* pBin) {
return pBin->name;
}

inline
int bin_quantity(const bin* pBin) {
return pBin->quantity;
}

inline
float bin_cost(const bin* pBin) {
return pBin->cost;
}

inline
void bin_destroy(const bin* pBin) {
}

inline
int bin_fprintf(FILE* fp, const bin* pBin) {
return fprintf(fp, "%s %d %f",
pBin->name, pBin->quantity, pBin->cost);
}
[color=blue]
> int main(int argc, char* argv[]) {
>[/color]
const bin cablebin = bin_create("Printer Cables", 25, 11.95);[color=blue]
>
> printf("A bin of %d %s at $%f a piece will cost $%f.\n",[/color]
bin_quantity(&cablebin), bin_name(&cablebin), bin_cost(&cable_bin),
bin_cost(&cablebin)*bin_quantity(&cablebin);

bin_destroy(&cablebin);[color=blue]
>
> return 0;
> }[/color]

CBFalconer
Guest
 
Posts: n/a
#7: Nov 14 '05

re: struct problem


jesse wrote:[color=blue]
>
> alo. i'm learning c at the moment, and i'm attempting to make
> little experiments with struct types. here's my code:
>
> #include <stdio.h>
>
> struct bin {
> char name[30];
> int quantity;
> float cost;
> };
>
> int main()
> {
> struct bin cablebin;
>
> cablebin.name = "Printer Cables";
> cablebin.quantity = 25;
> cablebin.cost = 11.95;
>
> printf("A bin of %d %s at $%f a piece will cost $%f.\n",
> cablebin.quantity, cablebin.name, cablebin.cost,
> cablebin.cost*cablebin.quantity);
>
> return 0;
> }
>
> for some reason, i get the following error:
> Error E2277 struct.c 13: Lvalue required in function main
>
> line 13 is: cablebin.name = "Printer Cables";
>
> i'm a bit confused; my code should be correct, so i don't know
> what's going.[/color]

You want:

strcpy(&(cablebin.name), "Printer Cables");

because arrays cannot be assigned, although structures can. You
can, however, assign pointers to char. So that if the definition
of the name field had been:

char *name;

your original assignment would have worked. Confused yet?

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Clark Cox
Guest
 
Posts: n/a
#8: Nov 14 '05

re: struct problem


In article <40340D49.CD618661@yahoo.com>,
CBFalconer <cbfalconer@yahoo.com> wrote:
[color=blue]
> jesse wrote:[color=green]
> >
> > alo. i'm learning c at the moment, and i'm attempting to make
> > little experiments with struct types. here's my code:
> >
> > #include <stdio.h>
> >
> > struct bin {
> > char name[30];
> > int quantity;
> > float cost;
> > };
> >
> > int main()
> > {
> > struct bin cablebin;
> >
> > cablebin.name = "Printer Cables";
> > cablebin.quantity = 25;
> > cablebin.cost = 11.95;
> >
> > printf("A bin of %d %s at $%f a piece will cost $%f.\n",
> > cablebin.quantity, cablebin.name, cablebin.cost,
> > cablebin.cost*cablebin.quantity);
> >
> > return 0;
> > }
> >
> > for some reason, i get the following error:
> > Error E2277 struct.c 13: Lvalue required in function main
> >
> > line 13 is: cablebin.name = "Printer Cables";
> >
> > i'm a bit confused; my code should be correct, so i don't know
> > what's going.[/color]
>
> You want:
>
> strcpy(&(cablebin.name), "Printer Cables");[/color]

What's wrong with:

strcpy(cablebin.name, "Printer Cables");

Jack Klein
Guest
 
Posts: n/a
#9: Nov 14 '05

re: struct problem


On Thu, 19 Feb 2004 02:00:41 GMT, CBFalconer <cbfalconer@yahoo.com>
wrote in comp.lang.c:
[color=blue]
> jesse wrote:[color=green]
> >
> > alo. i'm learning c at the moment, and i'm attempting to make
> > little experiments with struct types. here's my code:
> >
> > #include <stdio.h>
> >
> > struct bin {
> > char name[30];
> > int quantity;
> > float cost;
> > };
> >
> > int main()
> > {
> > struct bin cablebin;
> >
> > cablebin.name = "Printer Cables";
> > cablebin.quantity = 25;
> > cablebin.cost = 11.95;
> >
> > printf("A bin of %d %s at $%f a piece will cost $%f.\n",
> > cablebin.quantity, cablebin.name, cablebin.cost,
> > cablebin.cost*cablebin.quantity);
> >
> > return 0;
> > }
> >
> > for some reason, i get the following error:
> > Error E2277 struct.c 13: Lvalue required in function main
> >
> > line 13 is: cablebin.name = "Printer Cables";
> >
> > i'm a bit confused; my code should be correct, so i don't know
> > what's going.[/color]
>
> You want:
>
> strcpy(&(cablebin.name), "Printer Cables");[/color]

No you don't, surely. Incorrect pointer type. The type of
&(cablebin.name) is pointer to array of 30 characters, not pointer to
char.

Either:

&(cablebin.name[0])

....or even simpler,

cablebin.name
[color=blue]
>
> because arrays cannot be assigned, although structures can. You
> can, however, assign pointers to char. So that if the definition
> of the name field had been:
>
> char *name;
>
> your original assignment would have worked. Confused yet?[/color]

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Lewis Bowers
Guest
 
Posts: n/a
#10: Nov 14 '05

re: struct problem




"E. Robert Tisdale" wrote:
[color=blue]
> jesse wrote:
>[color=green]
> > I'm learning C at the moment and I'm attempting to [conduct] little
> > experiments with struct types. Here's my code:
> >
> > #include <stdio.h>[/color]
> #include <string.h>[color=green]
> >
> > typedef struct bin {
> > char name[30];
> > int quantity;
> > float cost;
> > } bin;[/color]
>
>[color=green]
> > printf("A bin of %d %s at $%f a piece will cost $%f.\n",[/color]
> bin_quantity(&cablebin), bin_name(&cablebin), bin_cost(&cable_bin),
> bin_cost(&cablebin)*bin_quantity(&cablebin);[/color]

Missing a ')' and since the float represents dollars and cents use %.2f

printf("A bin of %d %s at $%.2f a piece will cost $%.2f\n",
bin_quantity(&cablebin, bin_name(&cablebin, bin_cost(&cable_bin);
bin_cost(&cablebin)*bin_quantity(&cablebin));
[color=blue]
>
> bin_destroy(&cablebin);[color=green]
> >
> > return 0;
> > }[/color][/color]

Peter Shaggy Haywood
Guest
 
Posts: n/a
#11: Nov 14 '05

re: struct problem


Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
comp.lang.c.
struct problem's a cool scene! Dig it!
[color=blue]
>alo. i'm learning c at the moment, and i'm attempting to make little
>experiments with struct types. here's my code:
>
>#include <stdio.h>
>
>struct bin {
> char name[30];
> int quantity;
> float cost;
>};[/color]

On a different issue, float is a bad choice for a monitary amount.
It may not be able to accurately enough represent all the values
you're likely to need. It's better to use some sort of integral type
representing the number of cents rather than the number of dollars.
The conversion from cents to dollars and cents is trivial. Something
like this should do:

int dollars;
int cents;
....
dollars = cost / 100;
cents = cost % 100;

printf("The cost is $%d.%d\n", dollars, cents);

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Joe Wright
Guest
 
Posts: n/a
#12: Nov 14 '05

re: struct problem


Peter Shaggy Haywood wrote:[color=blue]
>
> Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
> comp.lang.c.
> struct problem's a cool scene! Dig it!
>[color=green]
> >alo. i'm learning c at the moment, and i'm attempting to make little
> >experiments with struct types. here's my code:
> >
> >#include <stdio.h>
> >
> >struct bin {
> > char name[30];
> > int quantity;
> > float cost;
> >};[/color]
>
> On a different issue, float is a bad choice for a monitary amount.
> It may not be able to accurately enough represent all the values
> you're likely to need. It's better to use some sort of integral type
> representing the number of cents rather than the number of dollars.
> The conversion from cents to dollars and cents is trivial. Something
> like this should do:
>
> int dollars;
> int cents;
> ...
> dollars = cost / 100;
> cents = cost % 100;
>
> printf("The cost is $%d.%d\n", dollars, cents);
>[/color]
Not quite. The % operator cannot be applied to float or double. Maybe..

cents = cost * 100;
dollars = cents / 100
cents %= 100;
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Peter Nilsson
Guest
 
Posts: n/a
#13: Nov 14 '05

re: struct problem


shaggy@australis.net.STOP.SPAM (Peter "Shaggy" Haywood) wrote in message news:<4036ba3d.786339@news.australis.net>...[color=blue]
> Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
> comp.lang.c.
> struct problem's a cool scene! Dig it!
>[color=green]
> >alo. i'm learning c at the moment, and i'm attempting to make little
> >experiments with struct types. here's my code:
> >
> >#include <stdio.h>
> >
> >struct bin {
> > char name[30];
> > int quantity;
> > float cost;
> >};[/color]
>
> On a different issue, float is a bad choice for a monitary amount.
> It may not be able to accurately enough represent all the values
> you're likely to need. It's better to use some sort of integral type
> representing the number of cents rather than the number of dollars.
> The conversion from cents to dollars and cents is trivial. Something
> like this should do:
>
> int dollars;
> int cents;
> ...
> dollars = cost / 100;
> cents = cost % 100;
>
> printf("The cost is $%d.%d\n", dollars, cents);[/color]

I'd shoot for %d.%02d, but as well as that, bare in mind negative
amounts are not unheard of, so it's important to consider them and
also the effects of division on negative operands. For instance, under
C90, a cost of -195 cents could print out as $-2.5 with the above
code.

--
Peter
Peter Shaggy Haywood
Guest
 
Posts: n/a
#14: Nov 14 '05

re: struct problem


Groovy hepcat Joe Wright was jivin' on Sun, 22 Feb 2004 14:20:20 GMT
in comp.lang.c.
Re: struct problem's a cool scene! Dig it!
[color=blue]
>Peter Shaggy Haywood wrote:[color=green]
>>
>> Groovy hepcat jesse was jivin' on Wed, 18 Feb 2004 19:29:21 GMT in
>> comp.lang.c.
>> struct problem's a cool scene! Dig it!
>>[color=darkred]
>> >alo. i'm learning c at the moment, and i'm attempting to make little
>> >experiments with struct types. here's my code:
>> >
>> >#include <stdio.h>
>> >
>> >struct bin {
>> > char name[30];
>> > int quantity;
>> > float cost;
>> >};[/color]
>>
>> On a different issue, float is a bad choice for a monitary amount.
>> It may not be able to accurately enough represent all the values
>> you're likely to need. It's better to use some sort of integral type
>> representing the number of cents rather than the number of dollars.
>> The conversion from cents to dollars and cents is trivial. Something
>> like this should do:
>>
>> int dollars;
>> int cents;
>> ...
>> dollars = cost / 100;
>> cents = cost % 100;
>>
>> printf("The cost is $%d.%d\n", dollars, cents);
>>[/color]
>Not quite. The % operator cannot be applied to float or double. Maybe..[/color]

What float or double? I said not to use float or double, but an
integral type. I guess I should have been clearer. I meant that cost
should be an int.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Eric Sosman
Guest
 
Posts: n/a
#15: Nov 14 '05

re: struct problem


Peter Shaggy Haywood wrote:[color=blue]
> [...]
> int dollars;
> int cents;
> ...
> dollars = cost / 100;
> cents = cost % 100;
>
> printf("The cost is $%d.%d\n", dollars, cents);[/color]

If `cost' is 109, this prints

The cost is $1.9

.... which might be a bit startling. I'd suggest changing
the second "%d" to "%02d".

--
Eric.Sosman@sun.com
Closed Thread


Similar C / C++ bytes