What does & mean?

This is a discussion on What does & mean? within the C Programming forums, part of the General Programming Boards category; Hi, May I ask a newbie question again? Why does the int a need to have a prefix of '&'? ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    What does & mean?

    Hi,

    May I ask a newbie question again? Why does the int a need to have a prefix of '&'? In java, I use readline() to read the inputstream only.

    Thanks for help.

    Code:
    #include <stdio.h>
    
    
    void main() {
    	int a, b;
        printf("Enter the number to be reversed.\n");
    	scanf("%d", &a);
    	do {
    		b = a % 10;
    		printf("%d", b);
    		a = a / 10;
    	}while(a != 0);
    	printf("\n");
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The & is the "address-of" operator. Whenever you call a function if you just pass the value of a ( no & ) then if the function modifys a it will not be changed when you return from the function. If you pass a pointer to a you can safely change the value and it will stay changed when you return.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Although I do not catch what did you mean, may I ask the following question to clarify the pointer issue?

    Code:
    void main() {
    	char *pw="west", *pe="east", *ps="South", *pn="North";
    	enum location {east=1, west=2, South=3, North=4};
    	enum location direction;
    	direction = west;
    
    	if(direction == west)
    		printf("Cannot go %s\n", pw);
    }
    Why the pw needs to add the prefix of '*'? Is it because pw points to a string object?

    If I printf("Cannot go %s\n", &pw), it returns 'D'. Why does it return 'D'?

    Thanks a lot

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The pw needs a * in front of it to signify its a pointer. The type is only char, you cannot fit "west" into a single character so you need a series ( or array ) or characters. The char * will sometimes put it in read-only memory so you may want to use an array if you plan on changing it later.

    Code:
    char buffer[] = "West";
    Whenever you are printing using the %s and &pw you will get strange results. %s expects a string and the address of pw is a memory location not a string.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    gogo: Java does not use pointers so that is why pointers seem difficult for you. Pointer are important to understand in C and C++.
    Mr. C: Author and Instructor

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have all forgotten the other use of the & symbol. Bitwise and.

    11100101 & 10010101 = 10000101

    Keep in mind, these are not whole numbers, ie, it is not 10,000,101, it is the bit wise reprentation of the number. The bitwise and is used to compare two values at the bit level and return what bits are set on both numbers.

    [edit]
    Oh, and since no one else covered it yet, main does not return void. You are not in Java any more. main always returns an integer in C. Always.
    [/edit]

    Quzah.
    Last edited by quzah; 03-10-2003 at 02:50 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21