Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: scanf design |
| Date | Mon, 12 Dec 2016 11:15:25 -0800 |
| Organization | None to speak of |
| Lines | 64 |
| Message-ID | <lnpokxq7qq.fsf@kst-u.example.com> (permalink) |
| References | <o2kh5p$e5q$1@dont-email.me> <96650663-827e-4f89-b78f-7b42a2516ef3@googlegroups.com> <o2mmfs$oag$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="5ae493c5c0eddd0692ad00c7942bf15f"; logging-data="16089"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9v9Gseooo5do3RhF6uy7w" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:42Gty8SMW7MexRXaXrTRDXnedYE= sha1:HWD8uwbNTgOCDEo7n4A6uABByqs= |
| Xref | csiph.com comp.lang.c:99112 |
Show key headers only | View raw
"Bill Cunningham" <nospam@nspam.invalid> writes:
> "Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message
> news:96650663-827e-4f89-b78f-7b42a2516ef3@googlegroups.com...
>> On Sunday, December 11, 2016 at 9:42:45 PM UTC, Bill Cuningham wrote:
>>> I have notice over time, and the last few days I have been thinking
>>> about this in particular. scanf's design. It wants the value of the
>>> address a pointer points to. Hum now I notice many functions want
>>> a pointer's address. But not so many an address pointed at.
>>>
>> No, it's rare. Normally we pass in values, either directly or via
>> arrays, and get the results back either in the function's return
>> type or in a buffer.
>> So normally pointers are named variables that point to buffers
>> of several values.
>>
>> But scanf is a bit unusual in that it returns a flexible number of
>> values of flexible type. So you see the & operator used heavily.
>> It's relatively rare to see the & operator used in most code.
I don't know how rare it is, but using the & operator certainly isn't
unusual. For example, one common convention is for functions to return
an int with the value zero to indicate success, and non-zero to indicate
failure, with any other information being passed back via pointers.
> So there's probably inside the body of scanf a lot of pointeres being
> used. I am going to use scanf more. I usually use fgets to stdin as the 3rd
> parameter. A bit odd probably.
> Here's how I usually use scanf when I have;
>
> int a;
> scanf("%d\n",&a);
>
> But I have never done this, if it's legal;
>
> int a;
> int *pa==&a;
> scanf("d\n",&pa);
>
> So that '&' being there. Is for for passing pointer and other values,
> addresses and type (int, double) values?
I presume you meant "%d\n" rather than "d\n". No, that's not at all
valid. &pa is the address of a pointer object; it's of type int**.
"%d" requires an argument of type int*.
You could legitimately write: scanf("%d\n", pa), which (given that
pa==&a) is equivalent to scanf("%d\n", &a).
It is possible to use scanf to read a pointer value from a text
representation, but that's hardly ever useful.
scanf is tricky. It's probably ok if you're not concerned about input
errors, but the corner cases will eventually bite you.
Oh, and you haven't said why you have that trailing \n in the format
string, but it probably doesn't do what you think it does. Hint: a
space would do exactly the same thing. I suggest reading the
documentation for scanf to find what what that is.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
scanf design "Bill Cuningham" <nospam@nspam.invalid> - 2016-12-11 16:42 -0500
Re: scanf design Noob <root@127.0.0.1> - 2016-12-11 22:55 +0100
Re: scanf design "Bill Cuningham" <nospam@nspam.invalid> - 2016-12-11 17:03 -0500
Re: scanf design Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 22:05 +0000
Re: scanf design Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-11 14:43 -0800
Re: scanf design "Bill Cunningham" <nospam@nspam.invalid> - 2016-12-12 12:25 -0500
Re: scanf design Keith Thompson <kst-u@mib.org> - 2016-12-12 11:15 -0800
Re: scanf design Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-12 13:17 -0800
Re: scanf design Keith Thompson <kst-u@mib.org> - 2016-12-12 14:02 -0800
Re: scanf design Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-12 15:06 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 15:58 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 16:10 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 16:10 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-12 16:22 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-15 07:30 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-15 07:56 -0800
Re: scanf design fir <profesor.fir@gmail.com> - 2016-12-15 08:21 -0800
Re: scanf design "Bill Cunningham" <nospam@nspam.invalid> - 2016-12-12 13:18 -0500
Re: scanf design Keith Thompson <kst-u@mib.org> - 2016-12-12 11:37 -0800
csiph-web