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 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 Online
      honey the codewitchH Online
      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 Online
            honey the codewitchH Online
            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
                            • 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

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

                              honey the codewitch wrote:

                              It's just another way of writing (foo + bar);

                              yes I know.

                              honey the codewitch wrote:

                              If you insist on not mixing pointer and array ops

                              Not sure what you mean. If I create an array of pointers then I would use it like an array. If I create a pointer to memory that contains sequential blocks (regardless of type) then I would use it like a pointer. The fact that syntactically the access is the same and tradable is not a consideration.

                              honey the codewitchH 1 Reply Last reply
                              0
                              • J jschell

                                honey the codewitch wrote:

                                It's just another way of writing (foo + bar);

                                yes I know.

                                honey the codewitch wrote:

                                If you insist on not mixing pointer and array ops

                                Not sure what you mean. If I create an array of pointers then I would use it like an array. If I create a pointer to memory that contains sequential blocks (regardless of type) then I would use it like a pointer. The fact that syntactically the access is the same and tradable is not a consideration.

                                honey the codewitchH Online
                                honey the codewitchH Online
                                honey the codewitch
                                wrote on last edited by
                                #22

                                jschell wrote:

                                Not sure what you mean.

                                I mean this

                                jschell wrote:

                                If I create an array of pointers then I would use it like an array. If I create a pointer to memory that contains sequential blocks (regardless of type) then I would use it like a pointer

                                So I think somewhere we started talking past each other.

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

                                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