Thread: Address of (&) Operator and its use

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    39

    Post Address of (&) Operator and its use

    Hello.
    i'm a newbie and i'm learning c... i had a question in mind in i thought here's a good place to ask...

    consider the following piece of code:
    Code:
    #include <stdio.h>
    void main (void)
    {
        char a[10];
        scanf("%s",a);
        printf("character: %s \n",a);
    
        int b;
        scanf("%d",&b);
        printf("int: %d \n",b);
    }
    my question is that, "logically" i can't understand why for scanf("%d",&b) "&b" has been used, and in scanf("%s",a), "a" without "&" operator has been used? although in printf("int: %d \n",b), b has been used without "&" operator...

    what's the use of & operator? i would appreciate it if someone could answer this question "logically"...
    i asked some of my colleges , and they told me that scanf() only accepts pointers, and when i use scanf() i should use & operator, i asked for reason, they replied that's the way it is... i can't find this answer logical... if that's the case? is it the same thing for printf(). if so, then why printf("int: %d \n",b); is not using "&" operator?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    & is the address of operator. The "address of" a variable is the same as a pointer to the value of that variable. As you hopefully understand, a pointer's value is the memory address where another value (of a certain type) can be found:
    Code:
    int n = 5, *ptr = &n;
    What has you confused here is the convention that the name of a string variable -- or, in fact, the name of any kind of array, which a C-string is a char array terminated with '\0' -- works as a pointer to the first element of the array.

    Code:
    char str[]="hey", *p = str;
    Notice no & needed here (in fact, it would be incorrect). You could use "*p = &str[0]" (the address of the first element).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    There is a very logical reason why scanf needs an address to an integer rather than the integer. It's because scanf reads the input and then *rerturns* the integer in the variable the pointer points to. If it wouldn't be a pointer, the called function wouldn't be able to write to the variable, as it could only modify a local copy. Imagine when you pass variables to your own functions and you change them in these functions: they change in the function, but not in the calling scope, because in the function copies rather than the actual variables are used.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    i understand now
    thanks

Popular pages Recent additions subscribe to a feed

Tags for this Thread