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 return a structure variable from a function ?

How to return a structure variable from a function ?

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
10 Posts 5 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.
  • N Offline
    N Offline
    Nikhil Trivedi
    wrote on last edited by
    #1

    Hello all, I am a newbie. I want to know how can I return a structure variable from a function to the calling function. I have a structure named httpsData which contains following fields: typedef struct { char httpServerName[25]; char httpReqHeaders[255]; char httpVerb[5]; char httpRetHeaders[255]; char httpRetMessage[2048]; }httpsData; I want to return a variable of type httpsdata. Can I use httpsData as function return type ? Thanks for your prompt answer. Nikhs Nikhil Trivedi

    P N G C 4 Replies Last reply
    0
    • N Nikhil Trivedi

      Hello all, I am a newbie. I want to know how can I return a structure variable from a function to the calling function. I have a structure named httpsData which contains following fields: typedef struct { char httpServerName[25]; char httpReqHeaders[255]; char httpVerb[5]; char httpRetHeaders[255]; char httpRetMessage[2048]; }httpsData; I want to return a variable of type httpsdata. Can I use httpsData as function return type ? Thanks for your prompt answer. Nikhs Nikhil Trivedi

      P Offline
      P Offline
      Parthi_Appu
      wrote on last edited by
      #2

      Nikhil Trivedi wrote:

      Can I use httpsData as function return type

      Sure you can use.

      struct httpsData _MyFunction(Param1,...)

      will do, but this is returing the struct as value type


      Do your Duty and Don't expect the Result

      1 Reply Last reply
      0
      • N Nikhil Trivedi

        Hello all, I am a newbie. I want to know how can I return a structure variable from a function to the calling function. I have a structure named httpsData which contains following fields: typedef struct { char httpServerName[25]; char httpReqHeaders[255]; char httpVerb[5]; char httpRetHeaders[255]; char httpRetMessage[2048]; }httpsData; I want to return a variable of type httpsdata. Can I use httpsData as function return type ? Thanks for your prompt answer. Nikhs Nikhil Trivedi

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        Nikhil Trivedi wrote:

        I want to return a variable of type httpsdata. Can I use httpsData as function return type ?

        Pass httpsdata as a reference... this helps you to return an execution status value too, which won't be possible if you are returning a structure.

        int GetHTTPSData( httpsdata& httpsData_o )
        {
        httpsData_o = anotherHttpsData;
        return 0; // All went well
        }


        Nibu thomas A Developer Programming tips[^]  My site[^]

        N 1 Reply Last reply
        0
        • N Nibu babu thomas

          Nikhil Trivedi wrote:

          I want to return a variable of type httpsdata. Can I use httpsData as function return type ?

          Pass httpsdata as a reference... this helps you to return an execution status value too, which won't be possible if you are returning a structure.

          int GetHTTPSData( httpsdata& httpsData_o )
          {
          httpsData_o = anotherHttpsData;
          return 0; // All went well
          }


          Nibu thomas A Developer Programming tips[^]  My site[^]

          N Offline
          N Offline
          Nikhil Trivedi
          wrote on last edited by
          #4

          Nibu babu thomas wrote:

          Pass httpsdata as a reference... this helps you return a execution status value too, which won't be possible

          After execution of the function, Can I access the elements which are modified during the function execution. If so how ? Thanks for your reply. Nikhs Nikhil Trivedi

          P 1 Reply Last reply
          0
          • N Nikhil Trivedi

            Nibu babu thomas wrote:

            Pass httpsdata as a reference... this helps you return a execution status value too, which won't be possible

            After execution of the function, Can I access the elements which are modified during the function execution. If so how ? Thanks for your reply. Nikhs Nikhil Trivedi

            P Offline
            P Offline
            Parthi_Appu
            wrote on last edited by
            #5

            Like this,

            struct __dummy {
            int i;
            int j;
            }Dummy;

            bool LoadValues(struct Dummy**&** src)
            {
            src.i = 1;
            src.j = 2;

            return true/false; /* Depends, assume here true */
            }

            void SumFns(..)
            {
            struct Dummy MyDummyValue = {0};
            if (LoadValues(MyDummyValue))
            {
            /* Succeeded */
            /* Now if you access 'MyDummyValue', you can have the data */
            int a = MyDummyValue.i; /* a will be assigned as 1 */
            int b = MyDummyValue.j; /* b will be assigned as 2 */
            }
            else
            /* Failed */
            }


            Do your Duty and Don't expect the Result

            N 2 Replies Last reply
            0
            • P Parthi_Appu

              Like this,

              struct __dummy {
              int i;
              int j;
              }Dummy;

              bool LoadValues(struct Dummy**&** src)
              {
              src.i = 1;
              src.j = 2;

              return true/false; /* Depends, assume here true */
              }

              void SumFns(..)
              {
              struct Dummy MyDummyValue = {0};
              if (LoadValues(MyDummyValue))
              {
              /* Succeeded */
              /* Now if you access 'MyDummyValue', you can have the data */
              int a = MyDummyValue.i; /* a will be assigned as 1 */
              int b = MyDummyValue.j; /* b will be assigned as 2 */
              }
              else
              /* Failed */
              }


              Do your Duty and Don't expect the Result

              N Offline
              N Offline
              Nikhil Trivedi
              wrote on last edited by
              #6

              Thanks to both of you, friends. This really helped me a lot. Thanks again Nikhs Nikhil Trivedi

              1 Reply Last reply
              0
              • N Nikhil Trivedi

                Hello all, I am a newbie. I want to know how can I return a structure variable from a function to the calling function. I have a structure named httpsData which contains following fields: typedef struct { char httpServerName[25]; char httpReqHeaders[255]; char httpVerb[5]; char httpRetHeaders[255]; char httpRetMessage[2048]; }httpsData; I want to return a variable of type httpsdata. Can I use httpsData as function return type ? Thanks for your prompt answer. Nikhs Nikhil Trivedi

                G Offline
                G Offline
                GameProfessor
                wrote on last edited by
                #7

                the short answer: yes the long answer: have u tried it ? The process of learning coding requires a lot of trial and error. You should have tried it to see if it works before asking such question.

                1 Reply Last reply
                0
                • N Nikhil Trivedi

                  Hello all, I am a newbie. I want to know how can I return a structure variable from a function to the calling function. I have a structure named httpsData which contains following fields: typedef struct { char httpServerName[25]; char httpReqHeaders[255]; char httpVerb[5]; char httpRetHeaders[255]; char httpRetMessage[2048]; }httpsData; I want to return a variable of type httpsdata. Can I use httpsData as function return type ? Thanks for your prompt answer. Nikhs Nikhil Trivedi

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

                  Is this the same problem as your question from yesterday[^] ? If yes, from where do you want to use this dll ? If it is not from a C++ program, then you'll need to pay extra consideration about converting the strings. For example if you use it in VB, the conversion is not trivial (as far as I remember).


                  Cédric Moonen Software developer
                  Charting control [v1.2 - Updated]

                  N 1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Is this the same problem as your question from yesterday[^] ? If yes, from where do you want to use this dll ? If it is not from a C++ program, then you'll need to pay extra consideration about converting the strings. For example if you use it in VB, the conversion is not trivial (as far as I remember).


                    Cédric Moonen Software developer
                    Charting control [v1.2 - Updated]

                    N Offline
                    N Offline
                    Nikhil Trivedi
                    wrote on last edited by
                    #9

                    Cedric Moonen wrote:

                    Is this the same problem as your question from yesterday[^] ?

                    Yes. It is the same problem. I want to modify my DLL and i would be using this DLL out of the C++ environment. This environment is also built up in C++. So I think It should work. I am trying to do so. Hope it would work. Thanks for your concern. Nikhs Nikhil Trivedi

                    1 Reply Last reply
                    0
                    • P Parthi_Appu

                      Like this,

                      struct __dummy {
                      int i;
                      int j;
                      }Dummy;

                      bool LoadValues(struct Dummy**&** src)
                      {
                      src.i = 1;
                      src.j = 2;

                      return true/false; /* Depends, assume here true */
                      }

                      void SumFns(..)
                      {
                      struct Dummy MyDummyValue = {0};
                      if (LoadValues(MyDummyValue))
                      {
                      /* Succeeded */
                      /* Now if you access 'MyDummyValue', you can have the data */
                      int a = MyDummyValue.i; /* a will be assigned as 1 */
                      int b = MyDummyValue.j; /* b will be assigned as 2 */
                      }
                      else
                      /* Failed */
                      }


                      Do your Duty and Don't expect the Result

                      N Offline
                      N Offline
                      Nikhil Trivedi
                      wrote on last edited by
                      #10

                      Hello dear friend, I am successful in calling the function with passing values as reference but when I access the values in calling application which is out side of the c++ environement, I am getting those as NULL. Can you tell me the reason. Nikhs Nikhil Trivedi

                      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