prototypes and struct quest.

  • Thread starter Bill Cunningham
  • Start date
K

Keith Thompson

Ralph Spitzner said:
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 :p

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.)
 
K

Keith Thompson

Ralph Spitzner said:
{
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.
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....

I don't think I read your previous reply, but that doesn't sound
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.)
 
K

Keith Thompson

Joel C. Salomon said:
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.
 
T

Tom St Denis

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
 
B

Bill Cunningham

Tom said:
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.


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


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
 
T

Tom St Denis

    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:

#define RADIUS = PI
RADIUS dorito = 3;

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
 
B

Bill Cunningham

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
 
T

Tom St Denis

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
you because all the other threads seemed well answered and I hadn't
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
 
B

Bill Cunningham

Tom St Denis wrote:

[...]
I lose sleep over nothing that happens in USENET. I only replied to
you because all the other threads seemed well answered and I hadn't
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
 
B

Bill Cunningham

Joel said:
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
 
B

Bill Cunningham

Joel said:
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.
[...]

Sorry meant 2000/100. I guess I was still wrong now that you've mentioned
integer arithmetic. ;-\ Hum I've heard of pointer arith. but not int
arithmetic.

B
 
M

Michael Angelo Ravera

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

B- Hide quoted text -

- Show quoted text -

I don't often post this kind of answer in a newsgroup, but it appears
that you are about 7 FUNDAMENTAL concepts behind in trying to tackle
C.

1) Understanding Memory sorage
2) Fundamentals of data representation
3) Different forms of arithmetic
4) Organization of functions
5) Use of operations
6) Fundamentals of recording persistent data
7) Appropriate file type selection
8) Fundamentals of logic

My suggestion is that you do an online "Crash Course" before you ask
for help again or post another message to this thread (Except to say
"Thank you, Mich!")

People on this forum are very willing to help, but they can't teach
you the language or all of its fundamental concepts that are
considered prerequisites to attempting your first program in C.
 
B

Bill Cunningham

Michael Angelo Ravera wrote:

I don't often post this kind of answer in a newsgroup, but it appears
that you are about 7 FUNDAMENTAL concepts behind in trying to tackle
C.

1) Understanding Memory sorage
2) Fundamentals of data representation
3) Different forms of arithmetic
4) Organization of functions
5) Use of operations
6) Fundamentals of recording persistent data
7) Appropriate file type selection
8) Fundamentals of logic

Oh boy I know there's different kinds of math but not different kinds of
arithmetic. If you mentioned recursive functions to me I would think of 1.
f(x) 2. a function that calls itself. But that's just about it.

Thanks Mich.
 
B

Bill Cunningham

Michael said:
I don't often post this kind of answer in a newsgroup, but it appears
that you are about 7 FUNDAMENTAL concepts behind in trying to tackle
C.

1) Understanding Memory sorage
2) Fundamentals of data representation
3) Different forms of arithmetic
4) Organization of functions
5) Use of operations
6) Fundamentals of recording persistent data
7) Appropriate file type selection
8) Fundamentals of logic

My suggestion is that you do an online "Crash Course" before you ask
for help again or post another message to this thread (Except to say
"Thank you, Mich!")

Ok would you recommend any particular sites? I will save this post as a
copy.

B
 
M

Michael Angelo Ravera

    Oh boy I know there's different kinds of math but not different kinds of
arithmetic. If you mentioned recursive functions to me I would think of 1..
f(x) 2. a function that calls itself. But that's just about it.

Thanks Mich.





- Show quoted text -

As re #3: whats 20000+30000? Using 16-bit signed arithmetic, the
answer is either "It's illegal" or "-17232" (not 50000).
What's 19/5? Using integer arithmetic, the answer is "4" (not 4.8).

As re #2? What is the most appropriate representation of a date? Well,
that depends upon whether you are going mostly to be just displaying
it or whehter you are going to be sorting with it or whether you might
want to caluculate with it.

If you are mostly just displaying the date, then whatever format
people who are using the program prefer to see the date is most
appropriate. "Friday, February Tenth Twenty-Eleven" would be just
fine, if your people like to see the date that way.

If however, you want to sort by it so that later dates occur before or
after earlier ones, depending, then you want to do something like
"20110210" (adding hyphens or slashes is OK, provided you always
include the zeros or use a leading space for mothns or days less than
10). You could have three different fields for year, month, and day in
that order, if you want.

If, in the third case, you might want to be able to calculate the
differences between the dates, you should either use the Julian Date
(number of days since the calendrical convergence) or take the number
of seconds since 1970 (or other convenient date) and either use it or
divide it by 86400.

In the last case, you have to use or find a function that shows the
dates for human consumption in a meaningful format and one that allows
input from humans in a convenient format (A date picker gadget?)

As re #1 (sorry for the typo), you need to understand how memory
storage is organized so that when you aggregate the data into a struct
and attempt to persist it, you will know what you are getting, so that
you can interpret the data that you retrieve from a persistent store
in the same way that you meant it when you persisted it.
 
R

Ralph Spitzner

Keith said:
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.

Sorry, about that. I think I was typing a bit too fast for my
brain :p
I don't think I read your previous reply, but that doesn't sound
like a good idea. For one thing, if you're allocating chunks from
a char array, alignment is likely to be a problem.

I was just trying to say that if you allocate space
for a struct, then why not have a char [SIZE] array,
in there, instead of having a separate char pointer.
(which you have to allocate separately)
Considering storing this stuff on disk and reading it
back into memory, without having to cope with the
'actual' length of the string.
Why re-implement malloc when the system provides it for you?
(I may have misunderstood your suggestion.)

Probably.
I just meant to say, have x many chars available
and watch that you don't cross limits is easier that to
malloc, or even realloc/copy data when you (for instance)
know that an extended description in an dvb eit event
cannot be longer than 4096 bytes....
(you can always, loose padded 0's, or just loose them
when you stuff that into a SQL db)

Errrm, seeing I'm probably loosing it here :p
In some cases it's just easier to have a fixed size
struct instead of juggling pointers.
(especially when it comes to free() that load again...)

-rasp
 
B

Bill Cunningham

Gordon said:
Then it obviously doesn't *READ*. Why didn't you name it spack()? or
sload()?



Isn't that what you just decided sread() does?


This contradicts what you said sread() is supposed to do above.


Try defining which function is supposed to do what, and give functions
reasonable names describing what they do, before trying to write code
for them.

Typing to fast for the brain sorry.

B
 
B

Bill Cunningham

Bill said:
Tom St Denis wrote:

[...]
I lose sleep over nothing that happens in USENET. I only replied to
you because all the other threads seemed well answered and I hadn't
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 other wording I guess you just had to start trolling.

B
 
B

Bill Cunningham

Michael Angelo Ravera wrote:
[...]
As re #2? What is the most appropriate representation of a date? Well,
that depends upon whether you are going mostly to be just displaying
it or whehter you are going to be sorting with it or whether you might
want to caluculate with it.

[...]

I want to calculate with this data. What seemed simple has now become to
complicated as a thread and has moved off topic and in every which
direction. I will work on this some myself.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top