Thread: addressof operator and dereferencing operator are used in place of each other

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    Cool addressof operator and dereferencing operator are used in place of each other

    Sorry I admit this is one of those BORING and basic questions, I apologize in advance, but I think since a pointer is nothing but an address, when talking about the addressof operator, the use of the asteric(*) would make much more sense... . Yep, I know it can't be changed and C was made 20+ years ago but I'm just going to see if you confirm me on this. So to put it in a brief snippet, I believe this:

    Code:
    //this is NOT the way C uses the symbols "*" and "&":
    int a = 150;
    //addressof a:
    int *ap = *a;
    //dereferenced ap:
    int apd = ≈
    could be much better use of those symbols(because now, pointer declaration and the addressof operator have the same symbols!).
    (PS: I know what pointers are and how they work, I was just trying to imagine myself explaining pointers to someone else, and then after some headache came up with this BOLD conclusion :P ).
    Last edited by narniat; 10-30-2018 at 04:13 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The usual mnemonic for dealing with * and & is that the declaration
    Code:
    int* ap;
    defines ap to be a pointer-to-int, but can also be read to say
    Code:
    int *ap;
    that "*ap" is an int, i.e., following the pointer gives you an int. Whether that's better or worse than your version is up for debate; the only advantage I'm claiming is that it matches the language.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Wow I had never looked at it from that angle! Interesting.

    That reminds me of another thing that also interests me and I think might be related to what you've said here: int* is claimed to be the type of ap when writing "int *ap", however, compiling the code below says something else:
    Code:
    int a,b,c;
    int* ap1,ap2,ap3;
    ap1=&a;
    ap2=&b;
    ap3=0; //an integer is intended, 0 is chosen randomly
    Now your point makes it much clearer, and thus I think int* is NOT the type of ap when writing "int *ap"!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by narniat
    I think int* is NOT the type of ap when writing "int *ap"!
    Except that you're wrong: the type of ap is indeed int*, and it must be so for the type of *ap to be int. The tricky situation where multiple variables are declared in the same statement has to do with the grammar, i.e., in that declaration, the * syntactically binds to the name declared, not to the type name. This still doesn't mean that the type of ap is not int*, because semantically that is the case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Well yes, I agree, semantically. But I'm almost sure that C is not specific about the symbol of a pointer-to-int type as it is about other types like int, char, etc. I think that's what I meant to say when I said:
    I think int* is NOT the type of ap when writing "int *ap"!
    Otherwise `int* ap1, ap2, ap3` should have worked the same as `int a, b, c` does. So (in my humble opinion), this is not a type symbol in C: int*. It's just a combination of two symbols together which indicates a pointer-to-int type(I may be wrong). So when you want to declare three of this type, you write: `int *ap1,*ap2,*ap3`.
    Last edited by narniat; 10-30-2018 at 06:59 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by narniat
    But I'm almost sure that C is not specific about the symbol of a pointer-to-int type as it is about other types like int, char, etc.
    Compile and run this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("A pointer to int is %zu bytes in size.\n", sizeof(int*));
        return 0;
    }
    Quote Originally Posted by narniat
    So (in my humble opinion), this is not a type symbol in C: int*.
    There is no such thing as a "type symbol" in C, unless you mean type name, but int* is indeed a type name, hence you can use it (when parenthesized) with sizeof.

    Quote Originally Posted by narniat
    It's just a combination of two symbols together which indicates a pointer-to-int type(I may be wrong).
    Every pointer type is a derived type, and by saying "indicates a pointer-to-int type", you have just described what a name of a type is, in particular, the name of the type that is known in English as pointer to int. In C, this is int*, i.e., int* is a type name, and this type name indicates (or identifies) the pointer-to-int type. You can say "it's just a combination of two symbols", but likewise int is just a combination of three symbols together which indicates an integer type. It so happens that because the pointer to int type is derived from this integer type, its type name is constructed from the type name of this integer type, i.e., int* is derived from int. It doesn't mean that int is a type name but int* isn't; they are both type names.

    Quote Originally Posted by narniat
    Otherwise `int* ap1, ap2, ap3` should have worked the same as `int a, b, c` does.
    (...)
    So when you want to declare three of this type, you write: `int *ap1,*ap2,*ap3`.
    The problem is that you're fixated on the notion that variables are always declared like this:
    Code:
    TYPENAME identifier0, identifier1, identifier2;
    This is a good way to introduce how to declare variables to beginners, but it is actually not the whole truth. For example, suppose we want to declare a variable named p that is a pointer to an array of 2 ints, without using a typedef:
    Code:
    int (*p)[2];
    This is because of the grammar of C. Hence, when you make an error concerning such a pointer or such an array, you might see your compiler giving you error messages that refer to a type that looks like int(*)[2].
    Last edited by laserlight; 10-30-2018 at 11:06 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Now that I look at it again, I see that you're right. It's just a C Grammar thing that the type int* has to be written in a scattered way when declaring pointers in that beginners way. But a type isn't originally a grammar thing, rather, it's the main concern of the COMPILER. A type doesn't even take a bit of an executable, that's just a compiler's help to the programmer.
    Saying that a type which the compiler is working with all the time and is referring to it all the time explicitly(e.g. in warnings, errors and etc.), because of a special case in the grammar is quite pointless.
    Thank you laserlight, the penny dropped specially when you talked about the int as a combination of three symbols(to convey your point).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-26-2016, 07:45 AM
  2. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  3. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  4. Replies: 1
    Last Post: 07-07-2008, 03:38 AM
  5. Overloading dereferencing operator.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2001, 09:53 PM

Tags for this Thread