Pointers equal non--&-ed values

This is a discussion on Pointers equal non--&-ed values within the C++ Programming forums, part of the General Programming Boards category; In the linked lists tutorial, it says pointers are equal to values without a & before them. Does the compiler ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    60

    Pointers equal non--&-ed values

    In the linked lists tutorial, it says pointers are equal to values without a & before them. Does the compiler like this idea or why doesn't it give errors?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It wouldn't give errors, because you can set a variable to whatever you like. The compiler doesn't understand the logic behind your program, just the syntax. It's like Microsoft Word won't tell you if you have a run-on sentence (well it never used to... it's getting pretty smart), because it doesn't understand what you're talking about, it only understand sentence structure and a dictionary. You can set pointers to anything you like, it's just that the idea of a pointer is based on assigning it a memory address. If you have a purpose for a pointer that doesn't need it to be a &'ed value, the compiler's fine with that - they're not that different from ordinary variables. Memory addresses are just numbers anyway!

    (of course, I haven't read that tutorial so the problem may just be that you're not understanding the -> operator, which will dereference for you - but hopefully this helps)

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Quote Originally Posted by Cprogramming Tutorial
    Code:
    conductor=conductor->next;
    conductor is a pointer and next is a pointer. I understand how a pointer could point to a pointer but it would need to have a & before it! Huh?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    no, & means "address of", since next already holds the address of something there is no need for the & there. Using & there would give you the address of next and not what next is pointing to.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Next is pointing to NULL there though???????!?!??!?!?!?!?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Am I ASCIIng a stupid question and getting a stupid ANSI, Salem?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There is nothing wrong with it pointing to NULL. Now if you tried to dereference it after that then you would have a problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of pointers and assignment within function
    By mmfuller in forum C++ Programming
    Replies: 11
    Last Post: 06-06-2008, 08:47 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  3. Displaying pointer's actual values in inheritance
    By Poof in forum C++ Programming
    Replies: 14
    Last Post: 07-29-2004, 07:34 AM
  4. Searching an array with pointers
    By pxleyes in forum C Programming
    Replies: 16
    Last Post: 03-23-2004, 04:07 PM

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