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. General Programming
  3. C / C++ / MFC
  4. &foo[bar] or (foo + bar) ?

&foo[bar] or (foo + bar) ?

Scheduled Pinned Locked Moved C / C++ / MFC
designquestioncsscomgraphics
22 Posts 9 Posters 104 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.
  • honey the codewitchH Offline
    honey the codewitchH Offline
    honey the codewitch
    wrote on last edited by
    #1

    what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

    K CPalliniC L J J 8 Replies Last reply
    0
    • honey the codewitchH honey the codewitch

      what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      You probably have to be a real C nerd to use (foo + bar). I'm not sure that anyone I work with would grok that they're both the same thing. But maybe I work with journeymen, and not craftsmen? For me, &foo[bar] gives me a brain cramp when I try to parse it. I'd prefer &(foo[bar]) just to make it obvious what is having it's address taken. Just to be pedantic.

      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

      J 1 Reply Last reply
      0
      • honey the codewitchH honey the codewitch

        what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

        Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        &bar[foo] :laugh:

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

        In testa che avete, signor di Ceprano?

        P 1 Reply Last reply
        0
        • honey the codewitchH honey the codewitch

          what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          No real preference, I use whichever seems (to me) to make most sense at the time.

          1 Reply Last reply
          0
          • K k5054

            You probably have to be a real C nerd to use (foo + bar). I'm not sure that anyone I work with would grok that they're both the same thing. But maybe I work with journeymen, and not craftsmen? For me, &foo[bar] gives me a brain cramp when I try to parse it. I'd prefer &(foo[bar]) just to make it obvious what is having it's address taken. Just to be pedantic.

            "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

            J Offline
            J Offline
            JudyL_MD
            wrote on last edited by
            #5

            Hail to the parenthesis. I always get comments that I use unnecessary parentheses in my code when I add them to pointer arithmetic or compound statements. My response is always along the lines of "yes I know and you were able to tell at a glance the order in which those statements were executed, and how the data was manipulated". Just put the damn () in there to make it easy for someone else to follow what you're trying to do without having to resort to the language specification. It's so much more maintainable for the next person, or yourself in 18 months when you go back to it.

            Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

            1 Reply Last reply
            0
            • honey the codewitchH honey the codewitch

              what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

              Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

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

              If the data type is defined as an array then use it as array in expressions. If the data type is defined as a pointer then use it as a pointer in expressions.

              honey the codewitch wrote:

              some people prefer &foo[bar]

              Rather certain that I am one that would not want to see that. I work to write code that is maintainable and using tricks just because the exist is not something that I look on favorably.

              honey the codewitchH 1 Reply Last reply
              0
              • J jschell

                If the data type is defined as an array then use it as array in expressions. If the data type is defined as a pointer then use it as a pointer in expressions.

                honey the codewitch wrote:

                some people prefer &foo[bar]

                Rather certain that I am one that would not want to see that. I work to write code that is maintainable and using tricks just because the exist is not something that I look on favorably.

                honey the codewitchH Offline
                honey the codewitchH Offline
                honey the codewitch
                wrote on last edited by
                #7

                That's not really a trick. It's "get me the address of the Nth array element." It's just another way of writing (foo + bar); If you insist on not mixing pointer and array ops than you'd be stuck with the very syntax you don't like in order to get the address of an array element, unless there's a 3rd way to do it I'm not considering?

                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                T J 2 Replies Last reply
                0
                • honey the codewitchH honey the codewitch

                  what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

                  Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                  J Offline
                  J Offline
                  JudyL_MD
                  wrote on last edited by
                  #8

                  &(foo[bar]) makes it real obvious you want the address of the _bar_th element of array foo

                  Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                  honey the codewitchH 1 Reply Last reply
                  0
                  • J JudyL_MD

                    &(foo[bar]) makes it real obvious you want the address of the _bar_th element of array foo

                    Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                    honey the codewitchH Offline
                    honey the codewitchH Offline
                    honey the codewitch
                    wrote on last edited by
                    #9

                    fair enough. Edit: To me the operation is obvious, such that using parentheses never even occurred to me. Clearly that's not the case with the other people that have commented here. ;P Not sure what that says about my code. :~

                    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                    J 1 Reply Last reply
                    0
                    • honey the codewitchH honey the codewitch

                      fair enough. Edit: To me the operation is obvious, such that using parentheses never even occurred to me. Clearly that's not the case with the other people that have commented here. ;P Not sure what that says about my code. :~

                      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                      J Offline
                      J Offline
                      JudyL_MD
                      wrote on last edited by
                      #10

                      It's obvious to me too, but I've been coding C since I upgraded from assembler back in the late 80's :-). Those who aren't as strong in C that have looked at my code always find it easier to understand pointer manipulation if I use array notation with those redundant (). They have more difficulty with foo + bar -- the idea that foo + 1 moves the pointer by (size of what foo points) bytes instead of 1 byte always blows their mind.

                      Be wary of strong drink. It can make you shoot at tax collectors - and miss. Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

                      1 Reply Last reply
                      0
                      • honey the codewitchH honey the codewitch

                        That's not really a trick. It's "get me the address of the Nth array element." It's just another way of writing (foo + bar); If you insist on not mixing pointer and array ops than you'd be stuck with the very syntax you don't like in order to get the address of an array element, unless there's a 3rd way to do it I'm not considering?

                        Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

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

                        So you say it is an array. Then use it as an array when you identify the element you want to take the address of.

                        honey the codewitchH 1 Reply Last reply
                        0
                        • T trønderen

                          So you say it is an array. Then use it as an array when you identify the element you want to take the address of.

                          honey the codewitchH Offline
                          honey the codewitchH Offline
                          honey the codewitch
                          wrote on last edited by
                          #12

                          that's exactly what &foo[bar] does. Literally it reads as take the address of the bar element of foo.

                          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                          T 1 Reply Last reply
                          0
                          • honey the codewitchH honey the codewitch

                            that's exactly what &foo[bar] does. Literally it reads as take the address of the bar element of foo.

                            Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

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

                            Right. So use that, rather than (foo + bar).

                            1 Reply Last reply
                            0
                            • honey the codewitchH honey the codewitch

                              what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

                              Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                              R Offline
                              R Offline
                              RedDk
                              wrote on last edited by
                              #14

                              Sum(foo, bar)

                              1 Reply Last reply
                              0
                              • CPalliniC CPallini

                                &bar[foo] :laugh:

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

                                P Offline
                                P Offline
                                Peter_in_2780
                                wrote on last edited by
                                #15

                                I used to write things like 2[array] to wind up certain colleagues. (Often the ones that insisted on putting the constant on the LHS of a comparison operator.)

                                Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                                L 1 Reply Last reply
                                0
                                • P Peter_in_2780

                                  I used to write things like 2[array] to wind up certain colleagues. (Often the ones that insisted on putting the constant on the LHS of a comparison operator.)

                                  Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  Peter_in_2780 wrote:

                                  the ones that insisted on putting the constant on the LHS of a comparison operator.

                                  :-O

                                  1 Reply Last reply
                                  0
                                  • honey the codewitchH honey the codewitch

                                    what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

                                    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                                    R Offline
                                    R Offline
                                    RedDk
                                    wrote on last edited by
                                    #17

                                    foo, bar, +

                                    T 1 Reply Last reply
                                    0
                                    • R RedDk

                                      foo, bar, +

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

                                      Did you intend to put an 'Enter' between 'foo' and 'bar'? If the comma was intended as an 'Enter', I guess that the comma between, 'bar' and '+' would have fouled it up.

                                      R 1 Reply Last reply
                                      0
                                      • T trønderen

                                        Did you intend to put an 'Enter' between 'foo' and 'bar'? If the comma was intended as an 'Enter', I guess that the comma between, 'bar' and '+' would have fouled it up.

                                        R Offline
                                        R Offline
                                        RedDk
                                        wrote on last edited by
                                        #19

                                        That is wrong, yes ... I was letting it slide because I was so late in on the thread that I figured no one would notice. Is it me or has the complexion of such changed lately? Anyway. What I meant to type was "foo,bar+". I'm no programmer just an antique calculator button presser. :sigh:

                                        1 Reply Last reply
                                        0
                                        • honey the codewitchH honey the codewitch

                                          what is your preference? I'm guessing some people prefer &foo[bar] because they may find the intent to be expressed more clearly. I find (foo + bar) to be more succinct with less steps, and therefore clearer to me.

                                          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

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

                                          One of the things that pisses me off in this book I bought about programming GPUs is that he presents so called "pseudocode" example for presenting his solutions. This pseudocode makes extensive use of C style pointer arithmetic, such as (foo + bar) without a word of mention; the reader is supposed to understand what it means, regardless of his programming background or preferred language. I do have enough background to understand it when I see it. But I see pseudocode as abstracted above one specific programming language, not just as an excuse for ignoring specific requirements of one chosen language. (foo+bar) is something you'll never find in any comparable language. It doesn't belong in any abstracted pseudocode. So does it belong in concrete code in one specific language? Well, if you strive to establish a tribal code, to distinguish between those who are in and those outside, this may be one of your tools. Those who say 'But that is an array, FGS! - Why don't you treat it as one?' - and recoil in horror. Maybe those could have been good developers for your project.

                                          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