UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » scanf() function

#1 2007-07-08 12:02 PM

aaronc
Member
Registered: 2007-04-25
Posts: 8

Re: scanf() function

Hi everyone,

This is just a very general question, I do not understand why sometimes we don't put the & in front of the arguments in scanf functions. I have the example below, I don't understand why in function getNumber we don't use & in front of scanf's argument number when checking using variable check. Please someone explain this.

int findFactors(int number)
3.5 - PROGRAM EXAMPLE – FINDING FACTORS
                                                 {
VERSION 2                                           /* Find all factors of number.
                                                       Return number found.
#include <stdio.h>                                  */
void getNumber(int *number);                        int count=0, check;
int findFactors(int number);
                                                    for (check=2; check<number; check++)
int main()                                          {
{                                                      if (number % check == 0)
    int count, number;                                 {
                                                          printf(“%d is a factor\n”, check);
    getNumber(&number);                                   count++;
    count = findFactors(number);                       }
                                                    }
    /* print count of factors found */              return count;
    printf(“\n%d has %d factors\n”,              }
           number, count);
    return 0;
}
void getNumber(int *number)
{
    /* read in number and do error checking */
    char temp;
    int check;
    do
    {
       printf(“\nPlease enter a +’ve integer “);
       check = scanf(“%d”, number);
       if (check != 1)
       {
          printf(“ERROR – Input invalid\n”);
          /* Clear keyboard buffer */
          do scanf(“%c”, &temp);
          while (temp != ‘\n’);
       }
    } while (check != 1 || *number < 0);
}

Offline

#2 2007-07-08 01:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: scanf() function

Because "number" at the scanf call is already a pointer to an int, and & takes the address of the variable instead of its value.

Offline

#3 2007-07-08 06:27 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: scanf() function

Right...  Or, to put it another way, you can see that the "&" is done already in the
call to the getNumber() function:

getNumber(&number);


So, you don't want to do it again...

(Of course, you could be dealing with dynamically allocated space, in which case
there might never be a need for any "&" done anywhere, too...  Because, in that
case, you'd always be working with just a pointer to dynamic space representing an
int (or whatever), rather than a real int variable on the stack (or static heap or
whatever)...)

Offline

  • Index
  • » C
  • » scanf() function

Board footer

Powered by FluxBB