Why use address-of operator (&)?? 
Author Message
 Why use address-of operator (&)??

Quote:

> Why we must use address-of operator (&) when reading numeric input using
> scanf( ) function?

> example :

> /* start source */
> char input;

> main ( )
> {
> printf("Enter a number : ");
> scanf ("%c", &input);
> return 0;
> }

> /* end of source */

> While we don't have to use & when reading string input using the same
> function.

> Steven Yik
> Beginner in C

Because you can only return data via a parameter list indirectly. When
you pass variables to a function - e.g.

int a,b,c;
/* ... more code... */
function1(a,b,c);

the *values* contained by a,b,c are passed. The called function then
has no knowledge of where a,b,c themselves exist in memory. This means
that the function itself cannot change the values of a,b,c. So if you
were to try, for example:

scanf("%d",a);

the scanf function would not know where a was to fill it with the
number entered in the input stream. Hopefully, your compiler will
barf too, because you're trying to use an integer parameter where
you should be using a pointer.

If, on the other hand, you had something like:

int a,b,c;
/* ... more code... */
function2(&a,&b,&c);

now, your are passing the addresses of a,b,c to your function rather
than their contents. This means that the function knows where they
are and can write to their addresses to change their values. Thus:

scanf("%d",&a);

will pass the address of a to scanf, scanf reads an integer from the
input stream and writes it to the address supplied, which is the
address of a in this case - thus scanf("%d",&a) puts the number input
into a.

The reason you don't have to pass strings with the & operator is that
strings are stored via pointers anyway, or in arrays, which are
converted to pointers when passed as function parameters - e.g.

char astring[40];
char *bstring;
bstring=malloc(40);   /* In a real situation NULL checking would be done
*/
scanf("%s",astring);
scanf("%s",bstring);

bstring is already a pointer - the address of the beginning of a string,
which is passed to scanf. astring is an array, the start address of
which is passed to scanf without any further need for the address-of
operator on your part.

- hope this makes sense !

Simon.



Fri, 11 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??
: Why we must use address-of operator (&) when reading numeric input using
: scanf( ) function?

: example :

: /* start source */
: char input;

: main ( )
: {
: printf("Enter a number : ");
: scanf ("%c", &input);
: return 0;
: }

: /* end of source */

: While we don't have to use & when reading string input using the same
: function.

Because the name of an array (string) defaults to a pointer when used
in a function call.

Will



Fri, 11 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??
Why we must use address-of operator (&) when reading numeric input using
scanf( ) function?

example :

/* start source */
char input;

main ( )
{
printf("Enter a number : ");
scanf ("%c", &input);
return 0;

Quote:
}

/* end of source */

While we don't have to use & when reading string input using the same
function.

Steven Yik
Beginner in C



Sat, 12 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??
In the simplest context a variable is passed to a procedure by value.  If
it is to be passed by reference and it isn't a pointer then the & needs be
used.  Otherwise the new value of the variable cannot be passed back when
the procedure called finishes execution and passes program control back to
the calling procedure eg.

This is not correct:

char input;
scanf("%c", input);

Input was passed by value and will not be changed because it is not a
pointer to a char.  The statement is not correct.

This is not correct too:

char input[1];
scanf("%c", &input);

The following statements are correct:

char input;
scanf("%c", &input);
--------------------------------------
char input[1];
scanf("%c",input);



Sun, 13 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??

Quote:

> In the simplest context a variable is passed to a procedure by value.  If
> it is to be passed by reference and it isn't a pointer then the & needs be
> used.  Otherwise the new value of the variable cannot be passed back when
> the procedure called finishes execution and passes program control back to
> the calling procedure eg.

> This is not correct:

> char input;
> scanf("%c", input);

> Input was passed by value and will not be changed because it is not a
> pointer to a char.  The statement is not correct.

> This is not correct too:

> char input[1];
> scanf("%c", &input);

> The following statements are correct:

> char input;
> scanf("%c", &input);

You meant
 scanf("%c", &input[0]);
I suppose?

Quote:
> --------------------------------------
> char input[1];
> scanf("%c",input);

--
Helmut


Sun, 13 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??

Quote:

> > The following statements are correct:

> > char input;
> > scanf("%c", &input);

> You meant
>  scanf("%c", &input[0]);
> I suppose?

> Helmut

  Look again, Helmut.  That 'input' thing is just a char, not a char* or a char
array.


Sun, 13 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??

Quote:


> > > The following statements are correct:

> > > char input;
> > > scanf("%c", &input);

> > You meant
> >  scanf("%c", &input[0]);
> > I suppose?

> > Helmut

>   Look again, Helmut.  That 'input' thing is just a char, not a char* or a char
> array.

Your right. Sorry.

I'm so used to "one name = one meaning"
that I read over the redefinition.
You could improve on your explanation
by chosing different variable names.
But this is no excuse.

--
Helmut



Sun, 13 May 2001 03:00:00 GMT  
 Why use address-of operator (&)??

Quote:

>> The following statements are correct:

>> char input;
>> scanf("%c", &input);

>You meant
> scanf("%c", &input[0]);
>I suppose?

Nope, he meant exactly what he wrote, and he was right.  Try to compile

    char input;
    scanf("%c", &input[0]);

and see what you get.  Most likely, something like this:

    /usr/lib/cmplrs/cc/cfe: Error: test.c, line 6: Subscripting a non-array
     scanf("%c", &input[0]);
     ------------------^

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - EP, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 13 May 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. C Question - pointer & address-operator

2. The & (address) operator and register allocation

3. Bits Counting (using & and | operators)

4. && operator question

5. anti-address operator

6. memcpy ( with address operator or not? )

7. Prototyping function to accept VB AddressOf operator

8. Address of vector::operator[n]() if instantiated with C-array type

9. Why no ||= and &&=

10. why no &&= and ||= ?

11. Why don't we have &&=, ||= operator?

12. Why only static operators and nonstatic indexers?

 

 
Powered by phpBB® Forum Software