Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #14720 > unrolled thread
| Started by | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| First post | 2011-12-18 20:51 -0500 |
| Last post | 2011-12-19 22:35 +0000 |
| Articles | 6 on this page of 106 — 24 participants |
Back to article view | Back to comp.lang.c
code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-18 20:51 -0500
Re: code snippet Geoff <geoff@invalid.invalid> - 2011-12-18 18:29 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-18 21:51 -0500
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-18 22:24 -0500
Re: code snippet Geoff <geoff@invalid.invalid> - 2011-12-18 19:53 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-18 23:32 -0500
Re: code snippet Geoff <geoff@invalid.invalid> - 2011-12-18 20:57 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-19 00:01 -0500
Re: code snippet Geoff <geoff@invalid.invalid> - 2011-12-18 21:13 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-19 16:42 -0500
Re: code snippet Keith Thompson <kst-u@mib.org> - 2011-12-18 20:14 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-18 23:27 -0500
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-19 12:33 +0000
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-19 16:33 -0500
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-20 00:20 +0000
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-19 19:46 -0500
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-20 01:14 +0000
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-20 15:51 -0500
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-21 14:11 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-21 07:03 -0800
Re: code snippet Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-21 21:41 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-21 16:35 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 01:03 +0000
Re: code snippet Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-22 03:19 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 07:11 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 16:59 +0000
Re: code snippet "io_x" <a@b.c.invalid> - 2011-12-22 18:38 +0100
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 10:40 -0800
Re: code snippet Markus Wichmann <nullplan@gmx.net> - 2011-12-22 19:12 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 03:50 -0800
Re: code snippet "io_x" <a@b.c.invalid> - 2011-12-28 17:06 +0100
Re: code snippet "io_x" <a@b.c.invalid> - 2012-01-01 20:04 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-01 12:38 -0800
Re: code snippet "io_x" <a@b.c.invalid> - 2012-01-02 10:14 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-03 04:56 -0800
Re: code snippet "io_x" <a@b.c.invalid> - 2012-01-03 18:26 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-04 00:09 -0800
Re: code snippet Ian Collins <ian-news@hotmail.com> - 2012-01-04 21:53 +1300
Re: code snippet "io_x" <a@b.c.invalid> - 2012-01-04 16:14 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-01 12:28 -0800
Re: code snippet "io_x" <a@b.c.invalid> - 2012-01-02 10:18 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-03 04:56 -0800
Re: code snippet "BartC" <bc@freeuk.com> - 2011-12-22 18:05 +0000
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 18:14 +0000
Re: code snippet superpollo <superpollo@tznvy.pbz> - 2011-12-22 20:00 +0100
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 19:08 +0000
Re: code snippet superpollo <superpollo@tznvy.pbz> - 2011-12-22 22:43 +0100
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 21:57 +0000
Re: code snippet BruceS <bruces42@hotmail.com> - 2011-12-23 16:26 -0800
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-22 19:21 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 11:33 -0800
Re: code snippet "io_x" <a@b.c.invalid> - 2011-12-23 09:10 +0100
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-23 14:43 +0000
Re: code snippet Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-23 17:14 +0000
Re: code snippet Keith Thompson <kst-u@mib.org> - 2011-12-23 12:10 -0800
Re: code snippet Walter Banks <walter@bytecraft.com> - 2012-01-03 14:24 -0500
Re: code snippet Kaz Kylheku <kaz@kylheku.com> - 2012-01-03 19:58 +0000
Re: code snippet Walter Banks <walter@bytecraft.com> - 2012-01-03 16:15 -0500
Re: code snippet blp@cs.stanford.edu (Ben Pfaff) - 2012-01-03 13:26 -0800
Re: code snippet Kaz Kylheku <kaz@kylheku.com> - 2012-01-03 22:04 +0000
Re: code snippet Ian Collins <ian-news@hotmail.com> - 2012-01-04 15:44 +1300
Re: code snippet "BartC" <bc@freeuk.com> - 2012-01-04 23:34 +0000
Re: code snippet James Harris <james.harris.1@googlemail.com> - 2012-01-04 15:57 -0800
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-05 23:34 -0800
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-06 00:40 -0800
Re: code snippet Kleuske <kleuske@nowhere.net> - 2012-01-06 10:33 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-06 04:30 -0800
Re: code snippet Ian Collins <ian-news@hotmail.com> - 2012-01-04 15:37 +1300
Re: code snippet Kaz Kylheku <kaz@kylheku.com> - 2012-01-04 03:37 +0000
Re: code snippet Ian Collins <ian-news@hotmail.com> - 2012-01-04 20:23 +1300
Re: code snippet jacob navia <jacob@spamsink.net> - 2011-12-22 18:26 +0100
Re: code snippet Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-22 19:34 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 11:57 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 20:18 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 12:52 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 20:37 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 13:27 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 21:57 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 14:09 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 21:57 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-23 04:01 -0800
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-22 14:24 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-22 21:57 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-01 23:06 -0800
Re: code snippet Hans Vlems <hvlems@freenet.de> - 2011-12-22 23:35 -0800
Re: code snippet "BartC" <bc@freeuk.com> - 2011-12-23 12:26 +0000
Re: code snippet jacob navia <jacob@spamsink.net> - 2011-12-23 18:32 +0100
Re: code snippet Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 04:04 -0800
Re: code snippet "io_x" <a@b.c.invalid> - 2011-12-23 09:10 +0100
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-25 11:13 -0800
Re: code snippet Seebs <usenet-nospam@seebs.net> - 2011-12-25 19:31 +0000
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-26 21:53 +0000
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-26 16:10 -0800
Re: code snippet Keith Thompson <kst-u@mib.org> - 2011-12-26 18:11 -0800
Re: code snippet 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-26 23:56 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-20 15:57 -0500
Re: code snippet Rui Maciel <rui.maciel@gmail.com> - 2011-12-21 12:39 +0000
Re: code snippet Bill Reid <hormelfree@gmail.com> - 2011-12-22 15:29 -0800
Re: code snippet Geoff <geoff@invalid.invalid> - 2011-12-19 19:19 -0800
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-20 13:51 -0500
Re: code snippet Geoff <geoff@invalid.invalid> - 2011-12-20 11:08 -0800
Re: code snippet Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-19 11:37 +0000
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-19 16:38 -0500
Re: code snippet "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-12-19 17:02 -0500
Re: code snippet "Bill Cunningham" <nospam@nspam.invalid> - 2011-12-19 17:21 -0500
Re: code snippet John Gordon <gordon@panix.com> - 2011-12-19 22:35 +0000
Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2011-12-20 11:08 -0800 |
| Message-ID | <r2n1f75tl9gdbdd679bu4hnep5uvm145ak@4ax.com> |
| In reply to | #14800 |
On Tue, 20 Dec 2011 13:51:12 -0500, "Bill Cunningham"
<nospam@nspam.invalid> wrote:
>Geoff wrote:
>
>> Why are you intent on avoiding structures?
>
> Well I guess it could work.
>
> Since a stock price, volume
>> and date are naturally carried together a structure would be the
>> natural object to keep them associated. Your get function becomes much
>> more simple if you avoid opening the file with the get function and
>> open the file from the caller or at a higher level and simply pass the
>> file pointer to it.
>>
>> #include <stdio.h>
>>
>> struct stock
>> {
>> double price;
>> long int date;
>> unsigned int number;
>> };
>>
>> int get_stock_data(FILE *fp, stock *pv)
>> {
>> int check;
>>
>> check = fscanf(fp, "%u %df %ul", &pv->number, &pv->price,
>> &pv->date);
>> if (check == EOF || check != 3)
>> {
>> printf("%d\n", ferror(fp));
>> clearerr(fp);
>> return 1;
>> }
>> return 0;
>> }
>
> Would that struct if not put into a header need to be extern if my other
>functions were in files of their own?
>
>
It would not be declared extern in this file but in other modules,
yes. But the proper place for it is in a header file, along with the
prototype for get_stock_data so your other modules understand the
interface to the function and the data it manipulates.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2011-12-19 11:37 +0000 |
| Message-ID | <0.59dc3ab0a0d82ce8744a.20111219113739GMT.877h1slrlo.fsf@bsb.me.uk> |
| In reply to | #14720 |
"Bill Cunningham" <nospam@nspam.invalid> writes:
> I have this code snippet that won't compile with gcc -c and I get errors
> of needing casts. This is untested code of course and I thought I'd ask for
> opinions on what I've done wrong.
I'll make a few general remarks...
> #include <stdio.h>
>
> void *get(void *pv)
> {
> double price;
> long int date;
> unsigned int number;
> int check;
> FILE *fp;
> if ((fp = fopen("num", "r")) == NULL) {
> perror("fopen error");
> return 1;
> }
> if ((check = fscanf(fp, "%u %f %l", &number, &price, &date)) == EOF) {
(you need %lf to read a double. %f is for float)
> printf("%d\n", ferror(fp));
> clearerr(fp);
> return 1;
> }
This is an example of what I'd call programming upside down. Look at
all the detail there: the file name, the format, the error messages, the
clearing of the IO error and so on, but *no code to do any real work*.
You've started walking, but you have no idea where you are going. A
clue to that is the function prototype. The name is entirely
noncommittal, and while there are valid uses of void *, here I think
they mean "I don't know what I need to pass to this function and I have
no idea what it should return".
When I program, I usually start with an outline of the overall behaviour
i.e. I start with main. This is broken down into functions that can do
the constituent parts of the job. Even these functions are often
written in outline first with the focus on what they actually do. The
parts you've worked out in detail, I will leave for later. For one
thing, I want to start testing as soon as possible, so I need a function
that works right away. If I find, either in testing or when finishing
these sketched functions, that things are going wrong, I can change the
design before I've sunk too much effort into it.
So my functions usually start out having a very specific name and
prototype:
double get_stock_price_on_date(FILE *fp, long int date)
but the body will often be what's called a stub:
double get_price_on_date(FILE *fp, long int date)
{
// Rewind file.
// Loop until third field equals date
// If date not found I'll return a negative price.
return 42;
}
Once it's all written like this I can start testing and filling in
the details as I go. If you can't do this, it is often because you
don't know, in enough detail, what your program is to do. It's almost
impossible to program with out a clear objective, so when I find myself
unable to sketch things out like this, it means I need to think more
about my objectives.
A detail: always test for fscanf success. As you say elsewhere the
returns are complex but if it returns 3 you got the three things you
need and you can do what you need to with them. You can (at least
initially) treat any return that is not 3 as an error. You can waste
ages dealing with all the various ways in which things have gone wrong,
so start instead with writing what happens when the input worked.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2011-12-19 16:38 -0500 |
| Message-ID | <4eefaeec$0$19273$bbae4d71@news.suddenlink.net> |
| In reply to | #14748 |
Ben Bacarisse wrote:
> "Bill Cunningham" <nospam@nspam.invalid> writes:
>
>> I have this code snippet that won't compile with gcc -c and I
>> get errors of needing casts. This is untested code of course and I
>> thought I'd ask for opinions on what I've done wrong.
>
> I'll make a few general remarks...
>
>> #include <stdio.h>
>>
>> void *get(void *pv)
>> {
>> double price;
>> long int date;
>> unsigned int number;
>> int check;
>> FILE *fp;
>> if ((fp = fopen("num", "r")) == NULL) {
>> perror("fopen error");
>> return 1;
>> }
>> if ((check = fscanf(fp, "%u %f %l", &number, &price, &date)) ==
>> EOF) {
>
> (you need %lf to read a double. %f is for float)
>
>> printf("%d\n", ferror(fp));
>> clearerr(fp);
>> return 1;
>> }
>
> This is an example of what I'd call programming upside down. Look at
> all the detail there: the file name, the format, the error messages,
> the clearing of the IO error and so on, but *no code to do any real
> work*. You've started walking, but you have no idea where you are
> going. A clue to that is the function prototype. The name is
> entirely noncommittal, and while there are valid uses of void *, here
> I think they mean "I don't know what I need to pass to this function
> and I have no idea what it should return".
>
> When I program, I usually start with an outline of the overall
> behaviour i.e. I start with main. This is broken down into functions
> that can do the constituent parts of the job. Even these functions
> are often written in outline first with the focus on what they
> actually do. The parts you've worked out in detail, I will leave for
> later. For one thing, I want to start testing as soon as possible,
> so I need a function that works right away. If I find, either in
> testing or when finishing these sketched functions, that things are
> going wrong, I can change the design before I've sunk too much effort
> into it.
>
> So my functions usually start out having a very specific name and
> prototype:
>
> double get_stock_price_on_date(FILE *fp, long int date)
>
> but the body will often be what's called a stub:
>
> double get_price_on_date(FILE *fp, long int date)
> {
> // Rewind file.
> // Loop until third field equals date
> // If date not found I'll return a negative price.
> return 42;
> }
The thing is your function always returns a double. I want my get to
return multiple types. The only 2 things I know to do is use a pointer of a
type that points to the location of stock price, or line number, or date.
The only other thing I know to do is pack the various types into one type. A
struct. Does this make sense?
> Once it's all written like this I can start testing and filling in
> the details as I go. If you can't do this, it is often because you
> don't know, in enough detail, what your program is to do. It's almost
> impossible to program with out a clear objective, so when I find
> myself unable to sketch things out like this, it means I need to
> think more about my objectives.
>
> A detail: always test for fscanf success. As you say elsewhere the
> returns are complex but if it returns 3 you got the three things you
> need and you can do what you need to with them. You can (at least
> initially) treat any return that is not 3 as an error. You can waste
> ages dealing with all the various ways in which things have gone
> wrong, so start instead with writing what happens when the input
> worked.
[toc] | [prev] | [next] | [standalone]
| From | "Morris Keesan" <mkeesan@post.harvard.edu> |
|---|---|
| Date | 2011-12-19 17:02 -0500 |
| Message-ID | <op.v6rbh31b5qv6o3@toshiba-laptop> |
| In reply to | #14765 |
On Mon, 19 Dec 2011 16:38:54 -0500, Bill Cunningham <nospam@nspam.invalid>
wrote:
...
>>> void *get(void *pv)
>>> {
...
> I want my get to
> return multiple types. The only 2 things I know to do is use a pointer
> of a type that points to the location of stock price, or line number,
> or date. The only other thing I know to do is pack the various types
> into one type. A struct. Does this make sense?
Look at this style, where you're returning a (void *) which points to
either an unsigned int, a long int, or a double. When another function
calls this get() function, how will it know what type is being returned?
If I were writing a function like this which could return multiple types,
I would have it return an enum which indicates which of the various types
is being returned. This enum could either be an element of a struct
whose other element is a union which could contain any one of the other
types, or I might pass in pointers to each of those types, and return
an enum type telling which of the pointers was used. I would also
define one or more error values for the enum object, which would
indicate that none of the data objects was populated.
--
Morris Keesan -- mkeesan@post.harvard.edu
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2011-12-19 17:21 -0500 |
| Message-ID | <4eefb902$0$11612$bbae4d71@news.suddenlink.net> |
| In reply to | #14748 |
Ben Bacarisse wrote:
> I'll make a few general remarks...
>
[code]
> This is an example of what I'd call programming upside down. Look at
> all the detail there: the file name, the format, the error messages,
> the clearing of the IO error and so on, but *no code to do any real
> work*. You've started walking, but you have no idea where you are
> going. A clue to that is the function prototype. The name is
> entirely noncommittal, and while there are valid uses of void *, here
> I think they mean "I don't know what I need to pass to this function
> and I have no idea what it should return".
>
> When I program, I usually start with an outline of the overall
> behaviour i.e. I start with main. This is broken down into functions
> that can do the constituent parts of the job. Even these functions
> are often written in outline first with the focus on what they
> actually do. The parts you've worked out in detail, I will leave for
> later. For one thing, I want to start testing as soon as possible,
> so I need a function that works right away. If I find, either in
> testing or when finishing these sketched functions, that things are
> going wrong, I can change the design before I've sunk too much effort
> into it.
>
> So my functions usually start out having a very specific name and
> prototype:
>
> double get_stock_price_on_date(FILE *fp, long int date)
>
> but the body will often be what's called a stub:
What is a stub? Never heard of it.
[snip]
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-12-19 22:35 +0000 |
| Message-ID | <jcoe7q$b3t$1@reader1.panix.com> |
| In reply to | #14775 |
In <4eefb902$0$11612$bbae4d71@news.suddenlink.net> "Bill Cunningham" <nospam@nspam.invalid> writes:
> What is a stub? Never heard of it.
A stub is an empty function. Its purpose is to serve as a placeholder
for the "real" function which you haven't written yet. It allows you to
design the high-level flow of your program, and have it compile, without
getting bogged down in low-level details.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [standalone]
Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]
Back to top | Article view | comp.lang.c
csiph-web