It's a pointer to a type, not a variable with an asterisk in its name
Matt Ellen-Tsivintzeli

Matt Ellen-Tsivintzeli @mellen

About: Ultra-fullstack software developer. Python, JavaScript, C#, C.

Location:
Earth
Joined:
May 2, 2017

It's a pointer to a type, not a variable with an asterisk in its name

Publish Date: Mar 16 '23
2 10

It's something that I've seen in C and C++ code. It doesn't make sense. I'm going to have a little rant about it.

When you are declaring a pointer, put the asterisk next to the type, because you are modifying the type.

int* x ✔ Yes very good.

int *x ❌ No very bad.

That's it.

The following is my reasoning in more detail for people who need more convincing because they're too stuck in their ways.

If I write int* x I am writing "x is of type 'pointer to int'".

If I write int *x I am writing "*x is of type int" which makes no sense.

I know the compiler doesn't care. I could write int*x and the compiler would eat it up like a good doggy. I care, because I'm reading the code.

* is not part of the variable name.

If I write x = &intvariable that is different to writing *x = &intvariable. The * is not part of the variable name.

If I have done typedef int* intptr; and then write intptr *x I have created a pointer to a pointer to an int, NOT a pointer to an int called *x.

THE * IS NOT PART OF THE VARIABLE NAME.

So don't make it look like it is.

Thank you for your time.

Comments 10 total

  • JoelBonetR 🥇
    JoelBonetR 🥇Mar 16, 2023

    You can also understand the asterisk as a prefix on the name to determine that int *x as X is a pointer storing a memory address of type int data or any other argument of your preference. That's the issue, that you can read and interpret it in different ways so it's a bit subjective... 😅

    I understand that sometimes the notation is confusing, because different textbooks place the * differently. The three following declarations are equivalent:

     int *x; 
     int* x; 
     int * x; 
    
    Enter fullscreen mode Exit fullscreen mode

    I always believed that the least confusing notation is int * x because when read from right to left you can interpret it as x is a pointer, hence allocating a memory address which references to an int value, plus you not "link" visibly the asterisk to the type nor to the var name.

    There are some details into that, see:

    int* x, y, z;
    
    Enter fullscreen mode Exit fullscreen mode

    This is 1 pointer and 2 int variables.

    int *x, y, z;
    
    Enter fullscreen mode Exit fullscreen mode

    This is still 1 pointer and 2 int variables.

    For the sake of clarity:

     int * x, * y, * z;
    
    Enter fullscreen mode Exit fullscreen mode

    Those are effectively 3 pointers and we can understand the asterisk as a somewhat independent (keyword if you will) and use it just like that.

    I haven't coded in C for around 10Y, maybe nowadays there is some linter out there that you can use to enforce one way or the other in your projects 😁

    • Matt Ellen-Tsivintzeli
      Matt Ellen-TsivintzeliMar 16, 2023

      Thank you for engaging in good faith.

      I completely disagree with everything you said.

      Apart from the bit about linters. Linters are great.

      • JoelBonetR 🥇
        JoelBonetR 🥇Mar 16, 2023

        😂😂 you are truly angry around this topic I see.

        I updated the comment above with a bit more of information around why I think that int * x is the least confusing, please check it.

        I don't think I'll be able to stress it further, though 😅

      • JoelBonetR 🥇
        JoelBonetR 🥇Mar 16, 2023

        PD: Just in case it's not clear in the text above I agree with you in that int* x is the worse way by far.

        Between the other two:

        int *x;
        int * x;
        
        Enter fullscreen mode Exit fullscreen mode

        it's just subjective opinion around readability and I won't care much if it's one or the other.

  • Paul J. Lucas
    Paul J. LucasMar 16, 2023

    Ritchie intentionally designed C's declaration syntax to be "the thing on the right, if it were used in an expression, has the type of the thing on the left." So int *x means *x when used in an expression has the type int — therefore, by implication, x must be a pointer to int.

    C is what it is. No amount of ranting will change it. Many before you have ranted about this same thing using the same arguments, so you're not bringing anything new to the table. If you're going to read other people's C code, you just have to get used to it. If if bothers you so much, you're free not to use C at all.

    Incidentally, it's trivial to make the counter argument: the * is not part of the type (no matter how badly you might want it to be), so don't make it look like it is.

    • Matt Ellen-Tsivintzeli
      Matt Ellen-TsivintzeliMar 17, 2023

      If that were the case then int * x would be a compiler error. Seems like Ritchie just made a mistake and forgot to point out that int* is a type, so everyone just kept making the same mistake.

      • Paul J. Lucas
        Paul J. LucasMar 17, 2023

        No, it wouldn't because whitespace largely insignificant. In your example, the "thing on the right" is still * x. The space doesn't matter.

        What's actually more annoying about C is that when you declare something like int f(int a[4]) that it's highly deceptive since C doesn't have array parameters.

  • wimachtendink
    wimachtendinkDec 3, 2023

    you are correct.

  • Taqmuraz
    TaqmurazDec 11, 2024

    I would like to read from you about C function pointers syntax. For me this syntax seems to be a sadistic joke.

  • xamidi
    xamidiDec 18, 2024

    I agree based on the same reasons. The same style is enforced by my C++ formatter via pressing Ctrl+Shift+F in Eclipse CDT (affects marked lines — or full file when nothing is marked).
    And I refuse to take code serious that doesn't follow such intuitive guidelines regarding code style. This is one of the most basic aspects of self-documenting code.

Add comment