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. Returning struct from functions syntax in VC++

Returning struct from functions syntax in VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++
3 Posts 3 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.
  • V Offline
    V Offline
    Vaclav
    wrote on last edited by
    #1

    Could anybody point me to a basic source of information on using struct in functions - passing as parameters and returning from functions. I am confused regarding the difference in C an C++ syntax. I am looking for using arrays of struct. Thanks for your time Vaclav

    C B 2 Replies Last reply
    0
    • V Vaclav

      Could anybody point me to a basic source of information on using struct in functions - passing as parameters and returning from functions. I am confused regarding the difference in C an C++ syntax. I am looking for using arrays of struct. Thanks for your time Vaclav

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Passing arrays is the hard bit, a struct is just like a class, you can pass it in and you can pass it out. If I wanted to return an array of items, I'd probably take a std::vector by reference, and add them to that. Christian Graus - Microsoft MVP - C++

      1 Reply Last reply
      0
      • V Vaclav

        Could anybody point me to a basic source of information on using struct in functions - passing as parameters and returning from functions. I am confused regarding the difference in C an C++ syntax. I am looking for using arrays of struct. Thanks for your time Vaclav

        B Offline
        B Offline
        basementman
        wrote on last edited by
        #3

        Structs are typically passed by pointer. In the case of struct arrays, it really depends on how you allocate the array. By incrementing a struct pointer, you actually increment the pointer to point to the next struct (the compiler increments it by the sizeof() the struct). EX:

        typedef struct _MyStruct
        {
        int iValue;
        char caData[21];
        } MyStruct;

        MyStruct *StructFcn(MyStruct *spStruct, int iCount);

        void PassStructToFcn()
        {
        MyStruct *spRetval = NULL;
        MyStruct sStruct;

        sStruct.iValue = 7;
        strcpy(sStruct.caData,"Hello");

        spRetval = StructFcn(&sStruct,1);
        }

        void PassStructArrayToFcn()
        {
        MyStruct *spRetval = NULL;
        MyStruct sStruct[3]; // array of 3 structs

        sStruct[0].iValue = 2;
        strcpy(sStruct[0].caData,"Hello");
        sStruct[1].iValue = 3;
        strcpy(sStruct[1].caData,"Hello");
        sStruct[2].iValue = 7;
        strcpy(sStruct[2].caData,"Hello");

        spRetval = StructFcn(&sStruct[0],3);
        }

        MyStruct *StructFcn(MyStruct *spStruct, int iCount)
        {
        // find the struct who's value is 7
        for (int iLup = 0; iLup < iCount)
        {
        if (spStruct->iValue == 7)
        return spStruct;

          spStruct++;
        }
        

        return NULL;
        }

        onwards and upwards...  -- modified at 12:58 Monday 12th December, 2005

        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