Navigation

Sunday 21 October 2018

Is a the same as &a in scanf in C language ?

#include <stdio.h>
int main(void)
{
    float rootbeer[10];

    scanf("%f", rootbeer);

    printf("%f\n", *rootbeer);

    return 0;
}
Look at the above code.
It works well.
In my understanding, rootbeer is the same as rootbeer + 0, which is pointer to a floating number.
Now I change rootbeer in scanf to &rootbeer.
#include <stdio.h>
int main(void)
{
    float rootbeer[10];

    scanf("%f", &rootbeer);

    printf("%f\n", *rootbeer);

    return 0;
}
I anticipated an error message because I think &rootbeer is pointer to array of 10 of float.
However, the changed code can also be compiled and works perfectly.
I wonder why.
Is a the same as &a in scanf in C language ?

Here

gcc -std=c11 -Wall -Werror ***.c   should be able to show all errors, including those undefined behaviour that still can be compiled.

No comments:

Post a Comment