Thread: value and address of variable in pointer program

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Hodor View Post
    The problem is that &foo is not an address... it's a pointer Going further, what does "address" mean anyway?
    Technically, it's an expression of pointer type, but that might be too pedantic of me. I only keep it straight because of course the address of foo (that is, using & on foo) is an address.

  2. #17
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    Technically, it's an expression of pointer type, but that might be too pedantic of me. I only keep it straight because of course the address of foo (that is, using & on foo) is an address.
    Hmm. Here I use & on foo and it's a pointer to int

    Code:
    > g++ --std=c++11 test.cpp 
    > ./a.out | c++filt -t
    int*
    Code:
    #include <typeinfo>
    #include <iostream>
    
    int main(void)
    {
        int foo;
        std::cout << typeid(&foo).name() << '\n';
    
        return 0;
    }

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, and I don't disagree with anything the compiler might say. This is why I admitted that it was a pedantic point. However, a pointer's value is a memory address. An "expression of pointer type" is precisely what &foo is, just like (n + 1) is an integer expression. If there is anything that I really disagree with, it is that using the word address when discussing pointers is confusing or wrong.

  4. #19
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    Yes, and I don't disagree with anything the compiler might say. This is why I admitted that it was a pedantic point. However, a pointer's value is a memory address. An "expression of pointer type" is precisely what &foo is, just like (n + 1) is an integer expression. If there is anything that I really disagree with, it is that using the word address when discussing pointers is confusing or wrong.
    I think we might think basically the same in that case

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, I'm pretty sure that we don't. Pointers don't store pointers, they store addresses. Saying that &foo is a pointer might actually "stop [someone from] saying address" but I don't really get where all the coy objection is going, at all.

    I made my point though, so whatever.
    Last edited by whiteflags; 01-08-2018 at 07:25 AM.

  6. #21
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    No, I'm pretty sure that we don't. Pointers don't store pointers, they store addresses. Saying that &foo is a pointer might actually "stop [someone from] saying address" but I don't really get where all the coy objection is going, at all.

    I made my point though, so whatever.
    The point is that for some reason people find the incredibly simple subject of pointers confusing (it really is so simple that you'd have to be an imbecile to not understand them). Adding an extra term that is not even accurate in a modern computing environment simply confuses them further because the two different concepts are, at the end of the day, the same thing; an address *is* a pointer. Using the term "address of" is silly because it doesn't even appear anywhere -- except in naming the & operator -- the C Standard.

    Edit: Next thing people will be saying that arguments to functions might be passed using "a stack" which is another example of something that is inaccurate (and another thing that the C standard doesn't mention at all because it's not necessary or accurate and might confuse the lemmings)

    Edit: Oh, and I'm pretty sure we do. Because in my own mind I consider &foo to be the address of foo. I just don't tell new people that because it confuses them
    Last edited by Hodor; 01-08-2018 at 06:33 PM.

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I hesitate to say that it's even a fair point.

    To be honest, I don't regard the C standard as a how-to manual, so it is unsurprising to me that it wouldn't necessarily include language like "address" or even "stack frame" very much. It's an unconvincing source to cite to avoid using those terms, unless you know that they aren't relevant to a specific platform or flavor of assembly. The C standard is light on many details, on purpose anyway. To say in so many words that addresses aren't mentioned in the standard means that addresses aren't really a thing is wrong. It means that you can explain a lot of language rules about pointers without explaining their reason for being or how they are used.

    I could say the same about the stack frame example. They can exist. The only reason I have ever seen someone mention stack frames is because they are trying to explain function arguments, or return variables, coming from an assembly point of view. That is a much more practical matter than the C standard was ever written to address.

  8. #23
    Registered User
    Join Date
    Jan 2018
    Posts
    2
    I agree with Hodor that the concept of pointers should be the least of a programmers problems when it comes to grasping concepts concerning the language. The hyped mystique surrounding them is almost comical.
    What I suspect is that a good deal of aspiring coders have zero insight on how a computer works on a marginally deeper level and they may also get overwhelmed by the language's syntax required for correctly handling them.
    '*', '&' and the way they're used seems to be too much of a hassle especially when former can have more complex usage patterns... enter function pointers(+pointer return values) and all goes down in flames even faster

    I would go as far as having somebody learn the basics of assembler and only then return to pointers in C to remove some of the haze leading them astray.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. Get address of variable from pointer
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2007, 10:50 AM
  3. obtaining a variable's address
    By Bleech in forum C Programming
    Replies: 5
    Last Post: 09-06-2006, 11:39 PM
  4. Storing an IP Address in a Variable?
    By guitarlover317 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2006, 07:54 AM
  5. may address of a variable be null
    By hebele in forum C Programming
    Replies: 5
    Last Post: 07-01-2005, 02:16 PM

Tags for this Thread