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 array as argument to a function

Passing an array as argument to a function

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
61 Posts 7 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.
  • L Lost User

    Arrays of pointers are just as trivial as arrays of anything, if you understand how to address them.

    fearless_ wrote:

    I`m getting an `illegal indirection` error

    Since we cannot see your screen we also cannot guess where that occurs. Please format your code properly and explain exactly where the error occurs.

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

    I get illegal indirection at this spot

    *ScreenLettersP_s[LetterVertexDataincrement]->position.x = i * letterwidth + ScreenLetterGroups[i].x

    L 1 Reply Last reply
    0
    • C Calin Negru

      I get illegal indirection at this spot

      *ScreenLettersP_s[LetterVertexDataincrement]->position.x = i * letterwidth + ScreenLetterGroups[i].x

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

      The variable ScreenLettersP_s is an array of pointers, so the reference ScreenLettersP_s[LetterVertexDataincrement] is one of the actual pointers. The leading asterisk on ScreenLettersP_s means an extra level of indirection which is not required (or valid).

      C 1 Reply Last reply
      0
      • L Lost User

        The variable ScreenLettersP_s is an array of pointers, so the reference ScreenLettersP_s[LetterVertexDataincrement] is one of the actual pointers. The leading asterisk on ScreenLettersP_s means an extra level of indirection which is not required (or valid).

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

        so this is specific for structures only. because if I change *data[i] = i * 2; to data[i] = i * 2; in the k5054s example I will be editing the save entry rather than the save data.

        L 1 Reply Last reply
        0
        • C Calin Negru

          so this is specific for structures only. because if I change *data[i] = i * 2; to data[i] = i * 2; in the k5054s example I will be editing the save entry rather than the save data.

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

          No. Pointers are pointers whatever they point at, be it an array or a structure. Think about a piece of memory as a sequence of cells. So a pointer to any cell allows you to access all the following cells in order, by using an index (pointer plus offset). If you (the programmer) have decided that the area you point to should be treated as if it contains different sized blocks (aka a structure), that does not affect the physical properties of the memory. It merely allows the compiler to calculate the distance between the elements of the structure. And an array of pointers is much the same thing. If you have trouble visualising multi levels of indirection, then always go for a single level. If you have an array of pointers, then create a temporary one and allocate an array entry to it like:

          CUSTOMVERTEX ** ScreenLettersP_s = new CUSTOMVERTEX* [NumberOfTextBuffers]; // an array of struct pointers
          CUSTOMVERTEX* pTemp = ScreenLettersP_s[0]; // get the first pointer in the array
          pTemp-> // now access the struct items.

          C 1 Reply Last reply
          0
          • L Lost User

            No. Pointers are pointers whatever they point at, be it an array or a structure. Think about a piece of memory as a sequence of cells. So a pointer to any cell allows you to access all the following cells in order, by using an index (pointer plus offset). If you (the programmer) have decided that the area you point to should be treated as if it contains different sized blocks (aka a structure), that does not affect the physical properties of the memory. It merely allows the compiler to calculate the distance between the elements of the structure. And an array of pointers is much the same thing. If you have trouble visualising multi levels of indirection, then always go for a single level. If you have an array of pointers, then create a temporary one and allocate an array entry to it like:

            CUSTOMVERTEX ** ScreenLettersP_s = new CUSTOMVERTEX* [NumberOfTextBuffers]; // an array of struct pointers
            CUSTOMVERTEX* pTemp = ScreenLettersP_s[0]; // get the first pointer in the array
            pTemp-> // now access the struct items.

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

            I understand. for the record that`s directx 9.

            L 1 Reply Last reply
            0
            • C Calin Negru

              I understand. for the record that`s directx 9.

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

              fearless_ wrote:

              directx 9

              I have not used DirectX, but I have used plenty of other Windows' functions that use structures, arrays of structures, and even arrays of structures that contain other unstructured structures. In the latter case, the presence or absence of certain items depends on settings elsewhere.

              C 1 Reply Last reply
              0
              • L Lost User

                fearless_ wrote:

                directx 9

                I have not used DirectX, but I have used plenty of other Windows' functions that use structures, arrays of structures, and even arrays of structures that contain other unstructured structures. In the latter case, the presence or absence of certain items depends on settings elsewhere.

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

                You have a vast experience, I appreciate all your help.

                L 1 Reply Last reply
                0
                • C Calin Negru

                  You have a vast experience, I appreciate all your help.

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

                  fearless_ wrote:

                  You have a vast experience

                  ~Far from it, what I don't know would fill many books. But given that I know some stuff, I am happy to help.

                  1 Reply Last reply
                  0
                  • C Calin Negru

                    Hi What is the syntax when you want to pass an array as argument? I`m looking for syntax for both function call and function definition.

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

                    Just as a conclusion to everything that has been said in this thread, if you want to pass an array as argument you have to declare it as pointer in the function definition.

                    K 1 Reply Last reply
                    0
                    • C Calin Negru

                      Just as a conclusion to everything that has been said in this thread, if you want to pass an array as argument you have to declare it as pointer in the function definition.

                      K Offline
                      K Offline
                      kalberts
                      wrote on last edited by
                      #61

                      How could you not? A C "array" IS a pointer to the start of it. There is nothing more to it. Stop thinking of C as a high level language. It is a CPU independent assembly language.

                      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