scanf and ampersand

V

vlsidesign

I am a newbie to C, and was hoping to get a little bit better handle
on this until I get deeper into pointers, etc. I kind of understand it
some, but still unfamiliar because I haven't got to pointers and using
them yet. Here is my program:

#include <stdio.h>
int main()
{
char yourname[10];
int yourworth;

printf("what's your name? ");
scanf(" %s", yourname);
printf("how many millions of dollars are you worth? ");
scanf(" %d", &yourworth);
printf("\n %s is worth %d \n", yourname, yourworth);
return 0;
}

I think that the scanf needs the second argument to be a pointer? I
believe that the value of a pointer is an memory address (which the &
ampersand address operator) retrieves? So when I put an ampersand in
front of the variable name "yourworth" that it returns the address,
which then the scanf then puts the value scanned into that memory
location? In the case of a string, which is just an array of
characters, like "yourname" above, it is really a pointer anyway, and
it's value is already an address, so the ampersand is not needed
anyway??
 
U

user923005

I am a newbie to C, and was hoping to get a little bit better handle
on this until I get deeper into pointers, etc. I kind of understand it
some, but still unfamiliar because I haven't got to pointers and using
them yet. Here is my program:

#include <stdio.h>
int main()
{
char yourname[10];
int yourworth;

printf("what's your name? ");
scanf(" %s", yourname);
printf("how many millions of dollars are you worth? ");
scanf(" %d", &yourworth);
printf("\n %s is worth %d \n", yourname, yourworth);
      return 0;

}

I think that the scanf needs the second argument to be a pointer? I
believe that the value of a pointer is an memory address (which the &
ampersand address operator) retrieves? So when I put an ampersand in
front of the variable name "yourworth" that it returns the address,
which then the scanf then puts the value scanned into that memory
location? In the case of a string, which is just an array of
characters, like "yourname" above, it is really a pointer anyway, and
it's value is already an address, so the ampersand is not needed
anyway??

From the C-FAQ:

6.3: So what is meant by the "equivalence of pointers and arrays"
in
C?

A: Much of the confusion surrounding arrays and pointers in C can
be traced to a misunderstanding of this statement. Saying
that
arrays and pointers are "equivalent" means neither that they
are
identical nor even interchangeable. What it means is that
array
and pointer arithmetic is defined such that a pointer can be
conveniently used to access an array or to simulate an array.

Specifically, the cornerstone of the equivalence is this key
definition:

An lvalue of type array-of-T which appears in an
expression decays (with three exceptions) into a
pointer to its first element; the type of the
resultant pointer is pointer-to-T.

That is, whenever an array appears in an expression,
the compiler implicitly generates a pointer to the array's
first element, just as if the programmer had written &a[0].
(The exceptions are when the array is the operand of a sizeof
or
& operator, or is a string literal initializer for a character
array.)

As a consequence of this definition, the compiler doesn't
apply
the array subscripting operator [] that differently to arrays
and pointers, after all. In an expression of the form a,
the
array decays into a pointer, following the rule above, and is
then subscripted just as would be a pointer variable in the
expression p (although the eventual memory accesses will be
different, as explained in question 6.2). If you were to
assign
the array's address to the pointer:

p = a;

then p[3] and a[3] would access the same element.

See also questions 6.8 and 6.14.

References: K&R1 Sec. 5.3 pp. 93-6; K&R2 Sec. 5.3 p. 99; ISO
Sec. 6.2.2.1, Sec. 6.3.2.1, Sec. 6.3.6; H&S Sec. 5.4.1 p. 124.

6.4: Then why are array and pointer declarations interchangeable as
function formal parameters?

A: It's supposed to be a convenience.

Since arrays decay immediately into pointers, an array is
never
actually passed to a function. Allowing pointer parameters to
be declared as arrays is a simply a way of making it look as
though an array was being passed, perhaps because the
parameter
will be used within the function as if it were an array.
Specifically, any parameter declarations which "look like"
arrays, e.g.

void f(char a[])
{ ... }

are treated by the compiler as if they were pointers, since
that
is what the function will receive if an array is passed:

void f(char *a)
{ ... }

This conversion holds only within function formal parameter
declarations, nowhere else. If the conversion bothers you,
avoid it; many programmers have concluded that the confusion
it
causes outweighs the small advantage of having the declaration
"look like" the call or the uses within the function.

See also question 6.21.

References: K&R1 Sec. 5.3 p. 95, Sec. A10.1 p. 205; K&R2
Sec. 5.3 p. 100, Sec. A8.6.3 p. 218, Sec. A10.1 p. 226; ISO
Sec. 6.5.4.3, Sec. 6.7.1, Sec. 6.9.6; H&S Sec. 9.3 p. 271;
CT&P
Sec. 3.3 pp. 33-4.
 
S

santosh

vlsidesign said:
I am a newbie to C, and was hoping to get a little bit better handle
on this until I get deeper into pointers, etc. I kind of understand it
some, but still unfamiliar because I haven't got to pointers and using
them yet. Here is my program:

#include <stdio.h>
int main()
{
char yourname[10];
int yourworth;

printf("what's your name? ");

Unless the output is terminated by a newline or fflush(stdout) is
called, the output may not immediately appear.
scanf(" %s", yourname);
printf("how many millions of dollars are you worth? ");
scanf(" %d", &yourworth);

The space character in the format string to scanf() is unnecessary.
printf("\n %s is worth %d \n", yourname, yourworth);
return 0;
}

I think that the scanf needs the second argument to be a pointer?
Yes.

I
believe that the value of a pointer is an memory address (which the &
ampersand address operator) retrieves?

Essentially yes. It could also be a null pointer value or indeterminate,
if uninitialised.
So when I put an ampersand in
front of the variable name "yourworth" that it returns the address,
which then the scanf then puts the value scanned into that memory
location?
Yes.

In the case of a string, which is just an array of
characters, like "yourname" above, it is really a pointer anyway, and
it's value is already an address, so the ampersand is not needed
anyway??

In most contexts in C an array name "degenerates" into a pointer value
to it's first element, i.e., &yourname[0] in this case. The exceptions
are when the array name is an operand to the address-of operator (&) or
the sizeof operator.

The comp.lang.c FAQ at http://www.c-faq.com has a lot of useful
information on common questions and misconceptions.
 
V

vlsidesign

From the C-FAQ:

6.3:    So what is meant by the "equivalence of pointers and arrays"
in
        C?

A:      Much of the confusion surrounding arrays and pointers in C can
        be traced to a misunderstanding of this statement.  Saying
that
        arrays and pointers are "equivalent" means neither that they
are
        identical nor even interchangeable.  What it means is that
array
        and pointer arithmetic is defined such that a pointer can be
        conveniently used to access an array or to simulate an array.
<snip>

My thinking was wrong .. there is a subtle but important
difference ... thanks.
 
K

Keith Thompson

vlsidesign said:
From the C-FAQ:

6.3:    So what is meant by the "equivalence of pointers and arrays"
in C?
[snip]

My thinking was wrong .. there is a subtle but important
difference ... thanks.

I wouldn't call the difference "subtle" at all. In fact, pointers and
arrays are two very different things. The difference only *seems*
subtle because the language goes out of its way to let you use them
interchangeably in many (but not all) contexts.

Consider this rather odd version of the classic hello world program:

#include <stdio.h>
int main(void)
{
char hello[] = "hello, ";
char *world = "world\n";
fputs(hello, stdout);
fputs(world, stdout);
return 0;
}

The rules of the language make it *appear* that the variables
``hello'' and ``world'' are the same kind of thing; the distinction
when you look at the C source code is indeed quite subtle. But if you
look past the syntax to the underlying semantics, there are two very
dissimilar things going on here. (Some might consider this a flaw in
the C language; I won't comment on that one way or the other.)

In my opinion, being able to look past the superficial syntax and
understand the actual underlying semantics is an important part of
being a skilled C programmer, as opposed to just being able to churn
out code that works most of the time. I'm referring here to the
abstract semantics defined by the language, not the machine-specific
stuff that can vary arbitrarily from one implementation to another
(though sometimes you need to know about that as well).

If you haven't already done so, I recommend reading all of section 6
of the comp.lang.c FAQ, www.c-faq.com. It does a good job of clearing
up this issue that's been a stumbling block for a lot of programmers
learning C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Scanf is being prioritized over printf ? 1
scanf internals 11
While loop unclear, can someone help? 4
question about scanf 11
about scanf() 2
scanf() 2
Problem with scanf 7
Function is not worked in C 2

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top