by Anonymous on December 22nd, 2008

Anonymous

Question

Help answer this question below.

What's the difference between &x and *x? I understand that *x is a pointer (like int *x; x is a pointer to int), but i don't understand how to use these and apply them in my codes. When would I use the & and when would I use the *?

  • Like
  • Report

Answers. 4 helpful answers below.

  • by GreenField on June 16th, 2009

    GreenField

    Example use:

    int *x ,a;

    a = 5;
    x= a; //x now contains the adress of a ,*x is equal to a (5)

    ++++++++++++++++++++++++++++++++++++++++++

    int a=1;
    int &x =a;

    //Now x is a reference to a
    //Whenever you change a ,x is changed
    //X is like atwin of a.

  • by M-Ateeq-R on April 25th, 2009

    M-Ateeq-R

    * is used to declare a pointer which point the memory location of variable suppose if you declare int *mypointer and other you have int x; and you write in next line mypointer=&x; it would get the memory adress useing pointer and then using & it would not print the memory adress it would print the value stored in x labeled memory space if you dont use & then it would show the memory name like 0xff84 etc

    No comments. Post one | Permalink

  • by JimmyG on December 27th, 2008

    JimmyG

    Here is Microsoft's take on it. Be sure to scroll down and read and think about the examples.

    http://msdn.microsoft.com/en-us/library/caaw7h5s(VS.71).aspx

  • by Retired From The Internet on September 16th, 2010

    Retired From The Internet

    & basically means "The address of".
    So if you have:

    int A = 5

    Then the compiler sets aside room in memory for an integer (let's say its at address 01) and sticks 5 into that location.

    Then if you print &A, it'll tell you it's 01

    * means "The value at"

    If print A, I'm asking for the value of A, which is 5
    If I do *(&A) I'm asking for the value at address 01, which is 5.
    if I just do *(A) then I'm asking for the value at 05 (the computer will probably crash)

    You can also declare a variable to just hold an address.

    int *B

    This time the compiler doesn't set aside room in memory for an integer, it sets aside room in memory for a memory address (which may or may not be the same size as an integer). That's called a pointer. B is the address and *B is the value at that address.

    People get confused because it looks like you are declaring an integer. It should really look like this: int* B. You are declaring a pointer, not an integer.

    No matter what you use before the *, the computer will set aside the same amount of memory. That's because the location of a variable is always the same size, no matter what type of variable is. (Just like a phone number is the same length, for a big house as well as a small house.)

    Now that you have *B you can say B = &A. It then puts the address of A (01) into B.
    Now, if you say *B = 6, you get the following:

    &B = some random memory address
    B = 01
    *B = 6

    &A = 01
    A = 6
    *A = value at address 06 (probably crashes computer)

    So when you changed *B, you also changed A.

    No comments. Post one | Permalink

Want to attach an image to your answer? Click here.

Did this answer your question? If not, then ask a new question or create a poll.

More Questions. Additional questions in this category.

You're reading What's the difference between &x and *x? I understand that *x is a pointer (like int *x; x is a pointer to int), but i don't understand how to use these and apply them in my codes. When would I use the & and when would I use the *?

Follow us on Facebook!

Related Ads

ANSWERBAG BUZZ

X c
C x
Int x c
Int x
X in c