[comp.lang.c] Probably an easy or dumb question

rds95@leah.Albany.Edu (Robert Seals) (08/10/89)

Hello frenz, is it workable to pass only the base address of an array
to "?scanf" and have it convert into successive memory locations?

int    d[4];

scanf("%d %d %d %d", d);

I guess the question is whether "scanf" uses the format string or
the number of arguments to determine how many thingies to convert.
So, what is it?

rob

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/10/89)

In article <1949@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes:
>int    d[4];
>scanf("%d %d %d %d", d);

Since the array name "d" is converted to "pointer to element d[0]"
before being passed to scanf(), the question answers itself.  d[0]
is a single int.  scanf() can't tell the difference between ways
of producing pointers-to-int that it is fed as arguments; they all
look the same to it.

>I guess the question is whether "scanf" uses the format string or
>the number of arguments to determine how many thingies to convert.

The format string and the input data determine how many items
scanf() actually converts.  Correct usage requires that you provide
one pointer argument for each potentially converted item, i.e. the
remaining arguments must exactly correspond to the format string.

jeffa@hpmwtd.HP.COM (Jeff Aguilera) (08/11/89)

> Hello frenz, is it workable to pass only the base address of an array
> to "?scanf" and have it convert into successive memory locations?
> 
> int    d[4];
> 
> scanf("%d %d %d %d", d);
> 

No.  Use

 scanf("%d %d %d %d", d, d+1, d+2, d+3);

For each valid conversion specification other than %%, at least one pointer 
is consumed from the stack.  You pass one pointer, but ask for four 
conversions.  Expect a hung system or dumped core, depending upon available
memory protection.

foessmei@lan.informatik.tu-muenchen.dbp.de (Reinhard Foessmeier) (08/11/89)

In article <1949@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes:
>...is it workable to pass only the base address of an array
>to "?scanf" and have it convert into successive memory locations?
>
>int    d[4];
>
>scanf("%d %d %d %d", d);
>
>I guess the question is whether "scanf" uses the format string or
>the number of arguments to determine how many thingies to convert.

"scanf" uzas la formatan vicon;         "scanf" uses the format string; it
^gi ne scias la nombron de argumentoj;  doesn't know about the # of args; but
tamen via propono ne funkcias.          what you propose doesn't work all the
La kawzo estas, ke "scanf" volas        same. The reason is that "scanf" wants
propran adreson por ^ciu legata         an address of its own for every datum
dateno.  ^Car la adreso de unu          read. Since the address of a single
elemento de estas distingebla de        element is not distinguishable from the
la adreso de vektoro, "scanf" ne        address of an array, "scanf" would be
scius kion fari el                      at a loss with something like

        scanf(" %d %d %d", d1, d2);

d1 kaj d2 povus esti deklaritaj kiel    d1 and d2 might be declared as

        int d1[2], d2[1];
aw                                      or
        int d1[1], d2[2];

"scanf" absolute ne scias, kiu el       "scanf" has no way of telling which of
la du deklaracio antawiris.             these declarations was used.
Reinhard F\"ossmeier, Technische Univ. M\"unchen |  Vivu
foessmeier@infovax.informatik.tu-muenchen.dbp.de |    la gefiloj
   [ { relay.cs.net | unido.uucp } ]             |       de niaj gepatroj!

chris@mimsy.UUCP (Chris Torek) (08/11/89)

In article <1949@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes:
>Hello frenz, is it workable to pass only the base address of an array
>to "?scanf" and have it convert into successive memory locations?
>
>int    d[4];
>
>scanf("%d %d %d %d", d);

Easy, yes; dumb, no: but the answer is no.

>I guess the question is whether "scanf" uses the format string or
>the number of arguments to determine how many thingies to convert.
>So, what is it?

It uses the format.  In particular, the proposed ANSI C standard says
that printf et al. can be given `extra' arguments, so these clearly
must use the format rather than (or perhaps in addition to) the
number and types of arguments; and given that, it is likely that
the same will be true of scanf, whether or not it is required (I
cannot recall offhand).

In any case, even if it used the number of arguments,

	scanf("%d %d %d %d", d);

would still pass only two arguments, one being a pointer to
char that points to the first `%', and the other being a pointer
to int that points to d[0].
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

dkelly@npiatl.UUCP (Dwight Kelly) (08/11/89)

In article <1949@leah.Albany.Edu> rds95@leah.Albany.Edu (Robert Seals) writes:
>Hello frenz, is it workable to pass only the base address of an array
>to "?scanf" and have it convert into successive memory locations?
>
>int    d[4];
>
>scanf("%d %d %d %d", d);
>

Not possible.  Scanf reads the variable addresses off of the stack.  In your
example, it would get the address of d[0] and then get three random addresses
for the next three %d.

One solution is:
  scanf("%d %d %d %d", &d[0], &d[1], &d[2], &d[3]);

or:

  int i;
  for (i=0; i<4; i++)
    scanf("%d", &d[i]);

--
Dwight Kelly            UUCP: gatech!npiatl!dkelly
Director R&D            AT&T: (404) 962-7220
Network Publications, Inc    2 Pamplin Drive     Lawrenceville, GA  30245
             Publisher of "The Real Estate Book" nationwide!