& argument question..

This is a discussion on & argument question.. within the C Programming forums, part of the General Programming Boards category; what is the meaning of & ? if i have int *p; &p is the address of p but is ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    & argument question..

    what is the meaning of & ?

    if i have
    int *p;
    &p is the address of p

    but is it just an argument
    or we can get the value in p by

    *(&p)

    ??

    i thought &p return
    is a string or a number which is the address of p (not type pointer)

    so if i look at &p as a function then
    it returns (non pointer value)
    correct?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by transgalactic2
    what is the meaning of & ?
    Bitwise and, or address-of operator.

    Quote Originally Posted by transgalactic2
    &p is the address of p
    Yes, but that is more generally true: if p is of type T, then &p is of type pointer to T.

    Quote Originally Posted by transgalactic2
    i thought &p return
    is a string or a number which is the address of p (not type pointer)
    The result of &p is the address of p, which is a pointer.

    Quote Originally Posted by transgalactic2
    so if i look at &p as a function then
    it returns (non pointer value)
    The address-of operator is not a function, and again, its result is a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so if i say *&p.value=7

    its just like saying p->value=7

    ??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by transgalactic2
    so if i say *&p.value=7

    its just like saying p->value=7
    Yes. In fact, in C, neither of the operators * and & are evaluated in that case.

    EDIT:
    Oh sorry, your mistake, and now mine as well. *&p.value is equivalent to p.value. It is not equivalent to p->value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but p.value
    has no meaning

    we need *p.value to get the value
    ??

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    * and & when sticked together cancel each other

    so whet i say *&p
    or *&p

    computer looks at them as p ??
    ??

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by transgalactic2
    but p.value
    has no meaning
    In the context of the code presented in your first post, that is true.

    Quote Originally Posted by transgalactic2
    we need *p.value to get the value
    If p.value has no meaning, then *p.value has no meaning, since it involves dereferencing the result of p.value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    * and & when sticked together cancel each other

    so whet i say *&p
    or *&p

    computer looks at them as p ??
    ??

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    I am not sure what you mean by "looks at them", but basically it treats *&p as if it were p.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok *& together cancel each other

    then if you say that &p and p are the same thing
    then why *&p and *p aren't the same thing
    ??

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by transgalactic2
    then if you say that &p and p are the same thing
    Where did I say that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    ok *& together cancel each other

    then if you say that &p and p are the same thing
    then why *&p and *p aren't the same thing
    ??
    &p and p are not the same thing. One is the address of p, the other is the value of p itself.

    Hence, *&p means "pointer to address of p", which is the same as p, since pointer to uses the value as an address to get to the actual thing, and address of makes a pointer of to a variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you said
    "if p is of type T, then &p is of type pointer to T"
    so &p is a pointer which holds the address of pointer p??

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by transgalactic2
    "if p is of type T, then &p is of type pointer to T"
    That's right. For example, if p is an int, then &p is an int*. If p is an int*, then &p is an int**.

    Quote Originally Posted by transgalactic2
    so &p is a pointer which holds the address of pointer p??
    &p is a pointer which holds the address of p.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    To clarify:

    &p = the address of the p variable
    *p = invalid, assuming that p is not a pointer. If p is a pointer, than dereferences the memory that p points to, e.g. reads the address of memory that p points to
    *&p = a dereferencing of the address of the p variable, equates to p
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  2. A stupid question...probably
    By Phoenix_Rebirth in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2007, 04:53 PM
  3. question about linklist
    By fj8283888 in forum C Programming
    Replies: 1
    Last Post: 04-14-2004, 06:13 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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