Search code examples
cfunctionpointersscanfampersand

Why don't I need ampersands with the scanf? (In C)


void getnums(int *a, int *b);

int main()   
{  
    int a;
    int b;
    int c;
    getnums(&a,&b);
    c = a + b;
    printf("a + b = %d\n", c);
    return 0;  
}

void getnums(int *a, int *b)
{ 
    printf("a:? ");
    scanf("%d", a);
    printf("b:? ");
    scanf("%d", b);
}

Why don't I need ampersands before the a and b in the scanfs? (The code currently works.)


Solution

  • Because scanf takes pointers as its arguments (so that it knows what variable to modify), and a and b are already pointers.