Velocity Reviews > prototypes and struct quest.

# prototypes and struct quest.

Keith Thompson
Guest
Posts: n/a

 02-09-2011
Ralph Spitzner <(E-Mail Removed)> writes:
> Bill Cunningham wrote:
>
>>>> int volume=0;
>>> can have -1 volume here

>>
>> I don't understand. Can have -1 value?

>
> Sorry, missed that one.
>
> int is is defined as *something* (depending on your architecture
> 8,16,32,64 bits) _signed_. So although it might holt that many bits
> the upper one is reserved for the sign.
> If your int is 8 bits long and and you have and order of 129 units
> you may end up sending them -1 units + receipt

int is guaranteed to be at least 16 bits.

The behavior of signed overflow is undefined. Attempting to
compute INT_MAX + 1 *might* yield INT_MIN, but it could do anything.
(This is particularly relevant in the presence of optimization.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson
Guest
Posts: n/a

 02-09-2011
Ralph Spitzner <(E-Mail Removed)> writes:
[...]
> {
> unsigned int dollar,cent;
>
> dollar = price / 100;
> cent = price % 100;
>
> printf("You have to pay \$%3d.2d\n",dollar,cent);
>
> }
>
> Will give you something a person can read/understand...

If price is 203, for example, the output will be:

You have to pay \$ 2. 3

Hmm. No, actually it will be:

You have to pay \$ 2.2d

since you forgot a '%'.

Try this:

printf("You have to pay \$%d.%02d\n", dollar, cent;

People are likely to spend more time reading the code you post
here than you spent writing it. Please consider testing it on your
system before posting.

>>As far as stack and heap
>> (memory) I think kernels handle that pretty well anyymore.

>
> That was in my last reply, it's far easier for memory management if
> you just allocate large chunks of continuous memory, instead
> of having small chunks all pointing to each other, hence the
> char array I mentioned...
> i.e. your struct is @0xffe0 whereas your name is @ 0x00fa,
> so memory can't be read in one go....

like a good idea. For one thing, if you're allocating chunks from
a char array, alignment is likely to be a problem.

Why re-implement malloc when the system provides it for you?

(I may have misunderstood your suggestion.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson
Guest
Posts: n/a

 02-09-2011
"Joel C. Salomon" <(E-Mail Removed)> writes:
> On 02/08/2011 02:06 PM, Bill Cunningham wrote:
>> int price=2000;
>> now int is 2000
>> printf("%d\n",2000/10); ?
>>
>> To make 20 USD ?

>
> No, because the expression 2000/100 is an integer expression. It’ll
> give you the right answer here, but the wrong type. Worse, by rules of
> integer arithmetic, 2054/100 gives 20, not 20.54.
>
> Try
> printf("%f\n", price/100.0);
> instead. Dividing by an explicitly floating-point number yields a
> floating-point number, and the “%f” format is what’s needed then.
>
> (Then look up printf() format flags to learn how to print a number with
> exactly two digits past the decimal point.)

When dealing with money, it's probably best not to use floating-point
arithmetic at all.

(I think there are ways to used floating-point reliably for money,
but it requires very careful programming.)

For example, if you store the price as an integer number of cents,
price/100 gives you the whole number of dollars, and price%100
gives you the remaining number of cents:

printf("The price is \$%d.%02d\n", price / 100, price % 100);

Note that Bill divided by 10, not by 100. The fact that this is a
very typical error for him suggests that Bill should not be writing
software that deals with money.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Tom St Denis
Guest
Posts: n/a

 02-09-2011
On Feb 9, 7:12*am, "Bill Cunningham" <(E-Mail Removed)> wrote:
> sread is supposed to take as it's parameter one value at a time and and put
> it into the struct stk type called name. I obviously don't remember how to
> do it correctly or neverlearned right. Hence my inexperience with structs.. I
> do believe I can do this with pointers though.

Except that in your example you provided as input a struct and
returned a struct, then your mock code copied elements out of a
struct. That's clearly not what you're trying to do.

What I don't get is why you bother trolling clc. If you were actually
trying to accomplish something you'd organize your thoughts and work a
lot better than you do. Instead, you just post random snippets of
whatever goof off material springs to mind.

> struct stk name;
>
> name.date=020811;
> name.price=20.00; //In USD.

You're sticking a floating point value in an unsigned int. Really?

> name.volume=232987;
>
> *This is how I would load a struct. Now swrite is supposed to do that for
> me. Instead of typing all this manually, I want to pass to swrite data and
> have swrite write it into the struct. sread would fetch it back. Simple
> concept. I don't know for sure how to code it.

Writing it should be easy, e.g.

void swrite(struct stk *dst, TYPE date, TYPE price, TYPE volume);

Or even

struct stk swrite(TYPE date, TYPE price, TYPE volume);

As for sread, that's a bit harder since you're trying to read out the
elements serially you either need multiple pointers like

void sread(struct stk *src, TYPE *date, TYPE *price, TYPE *volume);

Or you need to specify WHICH member you want to return. But what you
REALLY need to do is stop trolling USENET.

Tom

Bill Cunningham
Guest
Posts: n/a

 02-09-2011
Tom St Denis wrote:
> On Feb 9, 7:12 am, "Bill Cunningham" <(E-Mail Removed)> wrote:
>> sread is supposed to take as it's parameter one value at a time and
>> and put it into the struct stk type called name. I obviously don't
>> remember how to do it correctly or neverlearned right. Hence my
>> inexperience with structs. I do believe I can do this with pointers
>> though.

>
> Except that in your example you provided as input a struct and
> returned a struct, then your mock code copied elements out of a
> struct. That's clearly not what you're trying to do.
>
> What I don't get is why you bother trolling clc. If you were actually
> trying to accomplish something you'd organize your thoughts and work a
> lot better than you do. Instead, you just post random snippets of
> whatever goof off material springs to mind.
>
>> struct stk name;
>>
>> name.date=020811;
>> name.price=20.00; //In USD.

>
> You're sticking a floating point value in an unsigned int. Really?
>
>> name.volume=232987;
>>
>> This is how I would load a struct. Now swrite is supposed to do that
>> for me. Instead of typing all this manually, I want to pass to
>> swrite data and have swrite write it into the struct. sread would
>> fetch it back. Simple concept. I don't know for sure how to code it.

>
> Writing it should be easy, e.g.
>
> void swrite(struct stk *dst, TYPE date, TYPE price, TYPE volume);
>
> Or even
>
> struct stk swrite(TYPE date, TYPE price, TYPE volume);
>
> As for sread, that's a bit harder since you're trying to read out the
> elements serially you either need multiple pointers like
>
> void sread(struct stk *src, TYPE *date, TYPE *price, TYPE *volume);
>
> Or you need to specify WHICH member you want to return. But what you
> REALLY need to do is stop trolling USENET.
>
> Tom

I didn't know you were xenophobic Tom. If so why did you even respond to
my post. Well I thought I might need pointers somewhere I think your
examples have indeed helped me though so thanks.

Bill

Tom St Denis
Guest
Posts: n/a

 02-09-2011
On Feb 9, 12:57*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * I didn't know you were xenophobic Tom. If so why did you even respond to
> my post. Well I thought I might need pointers somewhere I think your
> examples have indeed helped me though so thanks.

I don't think that word means what you think it means...

I don't mind answering questions it's just you specifically are a pain
in that you don't ever think before you post. Your "example" programs
here [which aren't even complete apps] usually exhibit the lack of
effort on your part. Let me show you how your posts come across

---
BillS msg:

Hey guys I was trying to write a Quake 3D like engine, here's what I
have so far

struct potato { int gun; } = 5;

But so far I don't see any cool 3d models on the screen yet? What
gives?
---

Then two weeks later...

---
BillS msg:

Hey guys I was trying to write a program to calculate the mean density
of doritos. Here's what I have so far:

But so far it doesn't tell me what I want. Help?
---

etc and so on...You're a troll. And again I'm only replying because
I'm bored on lunch...

Tom

Bill Cunningham
Guest
Posts: n/a

 02-09-2011
Tom St Denis wrote:

[...]

> I don't mind answering questions it's just you specifically are a pain
> in that you don't ever think before you post.

Do you realize how hard it is for me to "think" as you call it. I have
lived in the same small town for my life and get lost going somewhere I go
everyday[by driving]. I see a psychiatrist MD and now a PhD in pschology to
trim down medications. I have been trimming some down slowly. Sometimes I
don't know exactly what I'm saying or even moreso explaining it coherently.
So I do understand I might be hard to understand. I have been working to
understand things more clearly before I jump in and post to clc. I come
across to some I know personally as "lazy".

Xenophobe: Usenet. Someone always looking for strange motives to others
posts. Worrying and fearing that someone might be doing something [like
trolling] and being so overly concerned they might "lose sleep" over
someone.

Bill

Tom St Denis
Guest
Posts: n/a

 02-09-2011
On Feb 9, 1:49*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Tom St Denis wrote:
>
> [...]
>
> > I don't mind answering questions it's just you specifically are a pain
> > in that you don't ever think before you post.

>
> * * Do you realize how hard it is for me to "think" as you call it. I have
> lived in the same small town for my life and get lost going somewhere I go
> everyday[by driving]. I see a psychiatrist MD and now a PhD in pschology to
> trim down medications. I have been trimming some down slowly. Sometimes I
> don't know exactly what I'm saying or even moreso explaining it coherently.
> So I do understand I might be hard to understand. I have been working to
> understand things more clearly before I jump in and post to clc. I come
> across to some I know personally as "lazy".

Not to sound like too much of a ****, but if you really do have a
disability that prevents you from being able to learn a technical
trade like programming why not just move on? It's like a blind man
"trying really hard" to be sharp shooter...

> Xenophobe: Usenet. Someone always looking for strange motives to others
> posts. Worrying and fearing that someone might be doing something [like
> trolling] and being so overly concerned they might "lose sleep" over
> someone.

I lose sleep over nothing that happens in USENET. I only replied to
picked a fight yet today. Plus I've seen your worthless posts before
and I felt like reminding you of how little you're contributing.

Tom

Bill Cunningham
Guest
Posts: n/a

 02-09-2011
Tom St Denis wrote:

[...]

> I lose sleep over nothing that happens in USENET. I only replied to
> picked a fight yet today. Plus I've seen your worthless posts before
> and I felt like reminding you of how little you're contributing.
>
> Tom

I'm not quite sure by what you mean by "...how little you're
contributing." I do lurk clc from time to time and I have taken some time
off to look at things and thought about whether I wanted to tackle C or
something else. I don't feel like I can contribute a wholelot to anyone else
starting C really because I don't feel worthy of interfering in the words of
those who understand much more than I.

B

Bill Cunningham
Guest
Posts: n/a

 02-09-2011
Joel C. Salomon wrote:
> On 02/08/2011 02:06 PM, Bill Cunningham wrote:
>> int price=2000;
>> now int is 2000
>> printf("%d\n",2000/10); ?
>>
>> To make 20 USD ?

>
> No, because the expression 2000/100 is an integer expression. It'll
> give you the right answer here, but the wrong type. Worse, by rules
> of integer arithmetic, 2054/100 gives 20, not 20.54.
>
> Try
> printf("%f\n", price/100.0);
> instead. Dividing by an explicitly floating-point number yields a
> floating-point number, and the "%f" format is what's needed then.
>
> (Then look up printf() format flags to learn how to print a number
> with exactly two digits past the decimal point.)
>
> -Joel

Already got that %.2f\n ! But I don't understand where you got the 2054.
What do you mean by int arithmetic?

B