Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. All in - pointer declaration

All in - pointer declaration

Scheduled Pinned Locked Moved The Lounge
39 Posts 19 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H honey the codewitch

    I hate the question. The reason being is that the pointer is part of the type intrinsically, and I want to use it that way but C isn't always consistent about it, so it gets weird no matter what you do. Pointer syntax is funky. It just is. There's no amount of style guidelines that will defunk pointers. Ergo, I do whatever the code around me does. Usually I put it with the type but I know I'm hated for that. :laugh:

    To err is human. Fortune favors the monsters.

    0 Offline
    0 Offline
    0x01AA
    wrote on last edited by
    #15

    I agree, see my comment below, I think similar mis feelings. Either way, I'm not worrying about such details. It is what it is and I close both eyes and go through it :laugh:

    1 Reply Last reply
    0
    • T trønderen

      If pointer is part of the type, make it! Make a typedef and use that when declaring variables.

      type* x, y;

      - is y of a pointer type? You know that it isn't. Lots of problems have been caused by making it appear as if x and y have the same type. (Thankfully, the compiler will catch most such wrongful assumptions.) If you change it to

      type y, *x;

      - is now the type definition for variable x split into two parts, separated by a variable declaration? If you make a typedef, you have a clear, all-in-one-place type definition, not cluttered up by variables. And you would avoid the risk of someone assuming, in the first example, that x and y are of the same type.

      H Offline
      H Offline
      honey the codewitch
      wrote on last edited by
      #16

      I generally agree with you, but I am not all in on that agreement, if that makes sense. Here's why: You have to look up a typedef to know what it is, and typedefs everywhere make it harder to know what's going on until you can adopt the fundamental lexicon that your typedefs essentially create. That said, everything you wrote is valid. I just think there are places where it might be overkill.

      To err is human. Fortune favors the monsters.

      T D 2 Replies Last reply
      0
      • C CPallini

        Type *p;

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        J Offline
        J Offline
        jmaida
        wrote on last edited by
        #17

        I use C and try to be consistent Type *p; example typedef struct { char form[LINE_SIZE]; // command form */ int narg; // number of arguments */ char error; // error code */ } LANG; LANG *LP;

        "A little time, a little trouble, your better day" Badfinger

        1 Reply Last reply
        0
        • Greg UtasG Greg Utas

          Type* p;

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          0 Offline
          0 Offline
          0x01AA
          wrote on last edited by
          #18

          I agree, but as mentioned in my post below... a thing I don't do but is very common:

          const char
          *a= "a",
          *b= "b";

          tells us, in this case we are something wrong... :(

          Greg UtasG 1 Reply Last reply
          0
          • 0 0x01AA

            I agree, but as mentioned in my post below... a thing I don't do but is very common:

            const char
            *a= "a",
            *b= "b";

            tells us, in this case we are something wrong... :(

            Greg UtasG Offline
            Greg UtasG Offline
            Greg Utas
            wrote on last edited by
            #19

            This doesn't bother me, because I never do this kind of thing.

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
            <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

            1 Reply Last reply
            0
            • M Mike Hankey

              Have always used this style.

              Type* p;

              PartsBin an Electronics Part Organizer - An updated version available! JaxCoder.com

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #20

              Me too. Although since I no longer develop in C++, I use:

              MyType p;

              :) /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              1 Reply Last reply
              0
              • S Single Step Debugger

                Type *p;

                or

                Type* p;

                or even

                Type * p;

                Me personally, I do whatever is the current company naming conventions.

                There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #21

                Single Step Debugger wrote:

                Type* p;

                As I recall it there was a column from I believe the 'C++ Users Journal' which pointed out that the following...

                int i;

                Is the same as

                So thus a pointer should be laid out as specified in the same way. That is if one needs a rationalization for it.

                T 1 Reply Last reply
                0
                • H honey the codewitch

                  I generally agree with you, but I am not all in on that agreement, if that makes sense. Here's why: You have to look up a typedef to know what it is, and typedefs everywhere make it harder to know what's going on until you can adopt the fundamental lexicon that your typedefs essentially create. That said, everything you wrote is valid. I just think there are places where it might be overkill.

                  To err is human. Fortune favors the monsters.

                  T Offline
                  T Offline
                  trønderen
                  wrote on last edited by
                  #22

                  Most programmers of *nix/C upbringing insist that #define constants are named in UPPER_CASE so that you can easily see from the name that it is a constant. Strangely enough, the majority of that very same group detests Hungarian blurb, even though the argument for the blurb is very much the same. Why isn't the conclusion identical? Well, the answer is not invented here ... I dislike both strongly. They seem fine for release 1.0. Then, as we experienced in one project, several of those static configuration parameters, #defines, were in release 2.0 made dynamically configurable, runtime modifiable. In those days we didn't have an IDE that could automatically rename a symbol throughout the project; it had to be done manually in every single file, and there were quite a few of them; it took some effort. So for quite some time, we had a number of all-uppercase variables. We experienced "Constants ain't. Variables won't." long before it became a standard rule. That project made me ask myself: Why really did I have to know at all times whether that value is constant or variable? Did it really affect my use of it? Should it? Constant-ness is sort of a "nice to know", but when it turns into a "need to know", you should stop and ask yourself: Do I really need to know? We had a very similar experience when porting code from 16 bits Windows 95 to 32 bits Windows XP, in the days when everyone spoke Hungarian. Lots of variable were expanded in size, and the renaming of them put on the todo list. Again, I asked myself (and my coworkers): Is it really significant, as seen from a problem solution point of view, whether this counter is 16 or 32 bits? Isn't it quite obvious that this other value is a string, both from is (blurbless) name and its use? Especially when moving code between different architectures, any blurb reflecting implementation (such as word length) is meaningless. For any semantics based blurb, you really don't gain much until you include, say, the struct type name in extenso - it obviously is a struct; you don't need a blurb for that! I have learned to program very much with disregard to the type definition; I don't have to look it up to see if it is a short, an int, a long or a longlong - it is large enough for its use. The float has sufficient precision for its use. If you are in doubt whether a value is a count (some sort of integer) or a measurement (some sort of float), then you should spend some time on understanding the solution at a conceptual level

                  1 Reply Last reply
                  0
                  • J jschell

                    Single Step Debugger wrote:

                    Type* p;

                    As I recall it there was a column from I believe the 'C++ Users Journal' which pointed out that the following...

                    int i;

                    Is the same as

                    So thus a pointer should be laid out as specified in the same way. That is if one needs a rationalization for it.

                    T Offline
                    T Offline
                    trønderen
                    wrote on last edited by
                    #23

                    Yeah, but in

                    int x, *y;

                    is the * part of the type declaration or the variable declaration? If it is part of the type declaration, then type of y is declared in two parts, with an interspersed (and rather irrelevant) variable declaration. Is that very rational? It is valid C!

                    J 1 Reply Last reply
                    0
                    • S Single Step Debugger

                      Type *p;

                      or

                      Type* p;

                      or even

                      Type * p;

                      Me personally, I do whatever is the current company naming conventions.

                      There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                      G Offline
                      G Offline
                      Gary R Wheeler
                      wrote on last edited by
                      #24

                      I've always preferred "Type  *p" over "Type*  p". To my mind the "pointer-ness" is a property of the variable and not the type. Of course, I've also hated this: typedef Type* TypePtr. If values are declared as TypePtr and you are using pointer-dereferencing with those values, the typedef obfuscates the original type. And before anyone pops up with "but what about...", I think typedef BaseType* OpaqueType is perfectly fine, when you're not using values of OpaqueType as pointers.

                      Software Zen: delete this;

                      Greg UtasG 1 Reply Last reply
                      0
                      • G Gary R Wheeler

                        I've always preferred "Type  *p" over "Type*  p". To my mind the "pointer-ness" is a property of the variable and not the type. Of course, I've also hated this: typedef Type* TypePtr. If values are declared as TypePtr and you are using pointer-dereferencing with those values, the typedef obfuscates the original type. And before anyone pops up with "but what about...", I think typedef BaseType* OpaqueType is perfectly fine, when you're not using values of OpaqueType as pointers.

                        Software Zen: delete this;

                        Greg UtasG Offline
                        Greg UtasG Offline
                        Greg Utas
                        wrote on last edited by
                        #25

                        A problem with

                        typedef Type* TypePtr;

                        is that if you declare

                        const TypePtr p;

                        it is p (the pointer itself) that is const, not Type. This can be confusing, so I avoid it.

                        Robust Services Core | Software Techniques for Lemmings | Articles
                        The fox knows many things, but the hedgehog knows one big thing.

                        <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                        <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                        G 1 Reply Last reply
                        0
                        • M Mircea Neacsu

                          type* (pointer is part of type) Now moving on: do you do "west const" or "east const"? Standard C++[^] (surely a lot of people - me included - don't want to do productive work today)

                          Mircea

                          H Offline
                          H Offline
                          honey the codewitch
                          wrote on last edited by
                          #26

                          constexpr static const int life_the_universe_and_everything = 42;

                          :-\

                          To err is human. Fortune favors the monsters.

                          M 1 Reply Last reply
                          0
                          • Greg UtasG Greg Utas

                            A problem with

                            typedef Type* TypePtr;

                            is that if you declare

                            const TypePtr p;

                            it is p (the pointer itself) that is const, not Type. This can be confusing, so I avoid it.

                            Robust Services Core | Software Techniques for Lemmings | Articles
                            The fox knows many things, but the hedgehog knows one big thing.

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #27

                            Good point. To my thinking const-ness, like pointer-ness, are properties of the variable and not the type. Part of my dislike for that sort of thing is people use some kind of naming convention (xxxPtr, xxxCPtr,...) that indicates the variant of the type. It pollutes the name space with additional identifiers you need to recognize. This replaces fundamental language syntax which is consistent by definition with arbitrary naming that may or may not be consistent. I've also noticed that the typedef overusers also tend to cast those types, often using language syntax, to other typedef's they've forgotten.

                            Software Zen: delete this;

                            Greg UtasG 1 Reply Last reply
                            0
                            • H honey the codewitch

                              constexpr static const int life_the_universe_and_everything = 42;

                              :-\

                              To err is human. Fortune favors the monsters.

                              M Offline
                              M Offline
                              Mircea Neacsu
                              wrote on last edited by
                              #28

                              Didn't expect less: west coast, shall be west const ;P

                              Mircea

                              1 Reply Last reply
                              0
                              • S Single Step Debugger

                                Type *p;

                                or

                                Type* p;

                                or even

                                Type * p;

                                Me personally, I do whatever is the current company naming conventions.

                                There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                                J Offline
                                J Offline
                                jmaida
                                wrote on last edited by
                                #29

                                Thanx for this introduction to computer language pointer constructs specifically related to C. True C++, C# are involved. The string of discussion was quite interesting.

                                "A little time, a little trouble, your better day" Badfinger

                                1 Reply Last reply
                                0
                                • S Single Step Debugger

                                  Type *p;

                                  or

                                  Type* p;

                                  or even

                                  Type * p;

                                  Me personally, I do whatever is the current company naming conventions.

                                  There is only one Vera Farmiga and Salma Hayek is her prophet! Advertise here – minimum three posts per day are guaranteed.

                                  D Offline
                                  D Offline
                                  den2k88
                                  wrote on last edited by
                                  #30

                                  I never met any convention that specifies pointer declarations so I use

                                  Type* p;

                                  I learnt with Type *p; but I always found it more complex to understand: after all that identifier holds a pointer to p, so it's type is pointer. Same for Type** p. Only sometimes I mix them around if there are readability reasons, for example Type** *p; can be in my opinion more readable if p holds a pointer to a matrix (i.e. if you need to return a matrix allocated by the callee, switch the matrix to send to the callee based on something, etc).

                                  GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                  1 Reply Last reply
                                  0
                                  • K k5054

                                    type *p;

                                    Because

                                    type* p, q

                                    doesn't do what it looks like it does. Of course, that kicks off the argument about multiple variables per type declaration.

                                    Keep Calm and Carry On

                                    D Offline
                                    D Offline
                                    den2k88
                                    wrote on last edited by
                                    #31

                                    k5054 wrote:

                                    Of course, that kicks off the argument about multiple variables per type declaration.

                                    Not an argument: don't do that.

                                    GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                    1 Reply Last reply
                                    0
                                    • H honey the codewitch

                                      I generally agree with you, but I am not all in on that agreement, if that makes sense. Here's why: You have to look up a typedef to know what it is, and typedefs everywhere make it harder to know what's going on until you can adopt the fundamental lexicon that your typedefs essentially create. That said, everything you wrote is valid. I just think there are places where it might be overkill.

                                      To err is human. Fortune favors the monsters.

                                      D Offline
                                      D Offline
                                      den2k88
                                      wrote on last edited by
                                      #32

                                      Working with autogenerated code from both MATLAB ans AutoSAR really teaches how much typedef and define complicate the code. Sometimes you have seven or eight redefinitions - it's Hell on Earth.

                                      GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                      R 1 Reply Last reply
                                      0
                                      • D den2k88

                                        Working with autogenerated code from both MATLAB ans AutoSAR really teaches how much typedef and define complicate the code. Sometimes you have seven or eight redefinitions - it's Hell on Earth.

                                        GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                        R Offline
                                        R Offline
                                        Rage
                                        wrote on last edited by
                                        #33

                                        AutoSAR ? You doing automotive development ?

                                        Do not escape reality : improve reality !

                                        D 1 Reply Last reply
                                        0
                                        • R Rage

                                          AutoSAR ? You doing automotive development ?

                                          Do not escape reality : improve reality !

                                          D Offline
                                          D Offline
                                          den2k88
                                          wrote on last edited by
                                          #34

                                          Yep, though I'm not touching AutoSAR since a couple of years - I moved to lower level peripherals that run on TLE987x and similar.

                                          GCS/GE d--(d) s-/+ a C+++ U+++ P-- L+@ E-- W+++ N+ o+ K- w+++ O? M-- V? PS+ PE Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                          R 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups