Probably an easy or dumb question 
Author Message
 Probably an easy or dumb question

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



Sun, 17 Jan 1993 13:30:00 GMT  
 Probably an easy or dumb question

Quote:

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

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


Sun, 17 Jan 1993 15:16:00 GMT  
 Probably an easy or dumb question

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



Sun, 17 Jan 1993 22:59:00 GMT  
 Probably an easy or dumb question

Quote:

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

   [ { relay.cs.net | unido.uucp } ]             |       de niaj gepatroj!



Sun, 17 Jan 1993 07:28:00 GMT  
 Probably an easy or dumb question

Quote:

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

Quote:
>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)



Sun, 17 Jan 1993 09:25:00 GMT  
 Probably an easy or dumb question

Quote:

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



Sun, 17 Jan 1993 14:02:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. This is a biztalk question but probably an easy question for you C# guru's

2. newbie question (and probably a dumb one)

3. Dumb (probably) C question...

4. Newbie Question - Probably Dumb as well

5. Probably the dumbest question there is...

6. Probably A Dumb CFormView Question...

7. probably a dumb question

8. probably an easy array, rand() question

9. Probably a easy question...

10. NEWBIE QUESTION PROBABLY EASY

11. EASY QUESTION FROM A DUMB BLOKE

12. Probably an easy one - MessageBox woes

 

 
Powered by phpBB® Forum Software