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. How to pass array to function?

How to pass array to function?

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresdebuggingtutorialquestion
4 Posts 4 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.
  • M Offline
    M Offline
    Ming Luo
    wrote on last edited by
    #1

    Hi all: I have the following codes: typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); } By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???

    Asura

    N M C 3 Replies Last reply
    0
    • M Ming Luo

      Hi all: I have the following codes: typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); } By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???

      Asura

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      You'll simply need to pass the size of the array as a separate argument. If you want to know more details on why this is happening, see this discussion[^], or search for "array decay" on the Internet.


      Programming Blog utf8-cpp

      1 Reply Last reply
      0
      • M Ming Luo

        Hi all: I have the following codes: typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); } By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???

        Asura

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        size_t size = sizeof(vertex_list) / sizeof(Point3); won't work since vertex_list is a pointer and its size is 4 (or 8). This... size_t size = sizeof(Object1_vertex) / sizeof(Point3); ...works for me (size == 3 if I initialize with 3 Point3 structs) but that makes passing the array useless since the array is accessible from anywhere in the same file anyway. You may need to pass the size along with the array pointer unless you can define the array with a set size. Mark

        1 Reply Last reply
        0
        • M Ming Luo

          Hi all: I have the following codes: typedef struct Point3Struct { double x, y, z; } Point3; ... Point3 Object1_vertex[] = {...} ... void someFunction(Point3 *vertex_list){ size_t size = sizeof(vertex_list) / sizeof(Point3); ... } int main(){ someFunction(Object1_vertex); } By passing the vertex array into the function "someFunction", I want to calculate the size of the array and do some other operations. But why this doesn't work? When I trace the program, the varialbe size is always 0. And the vertex_list is just the first element of the array. Other than that, if I try to do something like size_t size = sizeof(Object1_vertex) / sizeof(Point3); It will give me an error message saying that error C2070: 'Point3 []': illegal sizeof operand which complains about the "sizeof(Object1_vertex)". How should I solve the problem please? If I want to pass an array into a function, is this the correct way to do that???

          Asura

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          In addition to the previous answer, I suggest that you take a look at the container classes from the STL (std::list or std::vector for example). At first sight it might be more complicated but it solves a lot of problems (and finally ease your life because you won't need to manage the memory yourself).


          Cédric Moonen Software developer
          Charting control [v1.1]

          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