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. Passing an entry from an array as argument

Passing an entry from an array as argument

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 2 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    Can you pass an entry from the middle of the array as argument to a function?

    void AFunction(int * ArrayItem, int size)
    {

    }

    int TestArray[5];
    AFunction(&TestArray[3], 5);

    Greg UtasG 1 Reply Last reply
    0
    • C Calin Negru

      Can you pass an entry from the middle of the array as argument to a function?

      void AFunction(int * ArrayItem, int size)
      {

      }

      int TestArray[5];
      AFunction(&TestArray[3], 5);

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

      Sure, as a pointer to that element (&array[i]) or as a reference (array[i]). This is assuming you want access to the actual element, not a copy. If it's an array of int, the receiving arguments would have the types int* and int&, respectively.

      Robust Services Core | Software Techniques for Lemmings | Articles

      <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>

      C 1 Reply Last reply
      0
      • Greg UtasG Greg Utas

        Sure, as a pointer to that element (&array[i]) or as a reference (array[i]). This is assuming you want access to the actual element, not a copy. If it's an array of int, the receiving arguments would have the types int* and int&, respectively.

        Robust Services Core | Software Techniques for Lemmings | Articles

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #3

        What are the constraints? do you have access to other elements in the array in this situation? the function body in my example code is not defined because I`m not certain how it should look like.

        Greg UtasG 1 Reply Last reply
        0
        • C Calin Negru

          What are the constraints? do you have access to other elements in the array in this situation? the function body in my example code is not defined because I`m not certain how it should look like.

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

          No, this only gives you access to the element that you pass in. If you want access to all of them, pass the array itself (I seem to remember a previous thread about how to do that) and use the index operator.

          Robust Services Core | Software Techniques for Lemmings | Articles

          <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>

          C 1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            No, this only gives you access to the element that you pass in. If you want access to all of them, pass the array itself (I seem to remember a previous thread about how to do that) and use the index operator.

            Robust Services Core | Software Techniques for Lemmings | Articles

            C Offline
            C Offline
            Calin Negru
            wrote on last edited by
            #5

            Thanks Greg

            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