Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > prototypes and struct quest.

Reply
Thread Tools

prototypes and struct quest.

 
 
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.

[...]

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


 
Reply With Quote
 
 
 
 
Michael Angelo Ravera
Guest
Posts: n/a
 
      02-10-2011
On Feb 9, 2:47*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> 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- 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
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.
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-10-2011
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
> 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.

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



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-10-2011
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
> 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


 
Reply With Quote
 
Michael Angelo Ravera
Guest
Posts: n/a
 
      02-10-2011
On Feb 9, 4:09*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> 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
> > 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.
>
>
>
> > 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.- Hide quoted text -

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

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.

 
Reply With Quote
 
Ralph Spitzner
Guest
Posts: n/a
 
      02-11-2011
Keith Thompson wrote:

>
> 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

> 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
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




 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      02-11-2011
On 2011-02-09, Tom St Denis <(E-Mail Removed)> wrote:
> On Feb 9, 7:12?am, "Bill Cunningham" <(E-Mail Removed)> wrote:
>[...]


> What I don't get is why you bother trolling clc.


Because people respond.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-12-2011
Gordon Burditt 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.

>
> Then it obviously doesn't *READ*. Why didn't you name it spack()? or
> sload()?
>
>
>> 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.

>
> Isn't that what you just decided sread() does?
>
>> sread would fetch it back.

>
> This contradicts what you said sread() is supposed to do above.
>
>> Simple
>> concept. I don't know for sure how to code it.

>
> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-12-2011
Bill Cunningham wrote:
> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-16-2011
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
writeable buffer and struct.pack_into and struct.unpck_from Tzury Bar Yochay Python 4 09-21-2008 10:52 AM
Ruby/DL struct! method prototypes gregarican Ruby 1 10-13-2005 08:25 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Default arguments and prototypes Dave Vandervies C++ 5 10-03-2003 10:11 PM



Advertisments