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. asign to return value

asign to return value

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 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.
  • C Calin Negru

    struct TestStruct
    {
    int x;
    int z;

    };
    TestStruct S;
    TestStruct Funct()
    {
    return S;
    }

    //
    Funct().x = 100;

    Will this work?

    S Offline
    S Offline
    Stephane Capo
    wrote on last edited by
    #3

    Hello, the code itself will work, but will not do what I suppose you expect. Here, Funct returns a copy of the TestStruct S. So Funct().x = 100 will affect 100 in a temporary TestStruct that you can not use after that. I don't know why you want to use a function to access your S structure, but the behavior you look for is probably :

    // Funct returns a reference on S structure.
    TestStruct& Funct()
    {
    return S;
    }

    C 1 Reply Last reply
    0
    • S Stephane Capo

      Hello, the code itself will work, but will not do what I suppose you expect. Here, Funct returns a copy of the TestStruct S. So Funct().x = 100 will affect 100 in a temporary TestStruct that you can not use after that. I don't know why you want to use a function to access your S structure, but the behavior you look for is probably :

      // Funct returns a reference on S structure.
      TestStruct& Funct()
      {
      return S;
      }

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

      my actual code:

      ChartNode Chart[100];
      ChartNode NodeCoord(int x, int z)
      {
      return Chart[z * 10 + x];
      }

      if(NodeCoord(4,4).access)
      //do something
      NodeCoord(4,4).access = false;

      The result I`m looking for is the same as the result achieved with this function:

      void NodeCoord(int x, int z, bool writetobool)
      {
      Chart[z * 10 + x].access = writetobool;
      }

      S L 2 Replies Last reply
      0
      • C Calin Negru

        my actual code:

        ChartNode Chart[100];
        ChartNode NodeCoord(int x, int z)
        {
        return Chart[z * 10 + x];
        }

        if(NodeCoord(4,4).access)
        //do something
        NodeCoord(4,4).access = false;

        The result I`m looking for is the same as the result achieved with this function:

        void NodeCoord(int x, int z, bool writetobool)
        {
        Chart[z * 10 + x].access = writetobool;
        }

        S Offline
        S Offline
        Stephane Capo
        wrote on last edited by
        #5

        So you probably need to return a reference :

        ChartNode& NodeCoord(int x, int z)
        {
        return Chart[z * 10 + x];
        }

        else the NodeCoord(4,4).access = false will assign a temporary copy of your ChartNode struct and not the one in your array.

        C 1 Reply Last reply
        0
        • S Stephane Capo

          So you probably need to return a reference :

          ChartNode& NodeCoord(int x, int z)
          {
          return Chart[z * 10 + x];
          }

          else the NodeCoord(4,4).access = false will assign a temporary copy of your ChartNode struct and not the one in your array.

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

          ChartNode& NodeCoord() will work for both read and write?

          S 1 Reply Last reply
          0
          • C Calin Negru

            my actual code:

            ChartNode Chart[100];
            ChartNode NodeCoord(int x, int z)
            {
            return Chart[z * 10 + x];
            }

            if(NodeCoord(4,4).access)
            //do something
            NodeCoord(4,4).access = false;

            The result I`m looking for is the same as the result achieved with this function:

            void NodeCoord(int x, int z, bool writetobool)
            {
            Chart[z * 10 + x].access = writetobool;
            }

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

            If you are referring to array cells with two dimension values then you should use a two-dimensional array. As it is you can pass any values in to NodeCoord but there is no way of telling if they are valid. So you could end up with lots of random memory corruption.

            C 1 Reply Last reply
            0
            • L Lost User

              If you are referring to array cells with two dimension values then you should use a two-dimensional array. As it is you can pass any values in to NodeCoord but there is no way of telling if they are valid. So you could end up with lots of random memory corruption.

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

              Thanks Richard How do I declare a two (or more) dimensional array? The problem is I might need arrays with a great number of dimensions. I need a `one fits all` type of solution.

              L 1 Reply Last reply
              0
              • C Calin Negru

                Thanks Richard How do I declare a two (or more) dimensional array? The problem is I might need arrays with a great number of dimensions. I need a `one fits all` type of solution.

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

                Just like a one-dimensional, but you declare the two dimensions. Think of it as a block of items having some number of rows and columns. so you could have something like:

                #define MAXROW 10
                #define MAXCOL 5

                ChartNode Chart[MAXROW][MAXCOL];
                ChartNode NodeCoord(int row, int column)
                {
                if (row > 0 && row < MAXROW && // make sure row index
                column > 0 && column < MAXCOL) // and column index are within range
                {
                return Chart[row, column]; // return the cell
                }
                else
                {
                return NULL; // error
                }
                }

                All of these features are well documented in the C/C++ documentation and the various tutorials on the language.

                C 1 Reply Last reply
                0
                • C Calin Negru

                  ChartNode& NodeCoord() will work for both read and write?

                  S Offline
                  S Offline
                  Stephane Capo
                  wrote on last edited by
                  #10

                  Yes.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Just like a one-dimensional, but you declare the two dimensions. Think of it as a block of items having some number of rows and columns. so you could have something like:

                    #define MAXROW 10
                    #define MAXCOL 5

                    ChartNode Chart[MAXROW][MAXCOL];
                    ChartNode NodeCoord(int row, int column)
                    {
                    if (row > 0 && row < MAXROW && // make sure row index
                    column > 0 && column < MAXCOL) // and column index are within range
                    {
                    return Chart[row, column]; // return the cell
                    }
                    else
                    {
                    return NULL; // error
                    }
                    }

                    All of these features are well documented in the C/C++ documentation and the various tutorials on the language.

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

                    the two dimensional array seems like something too good to be true :) The compiler is definitely doing something suspicious behind the courtain. Thanks.

                    L _ 2 Replies Last reply
                    0
                    • C Calin Negru

                      the two dimensional array seems like something too good to be true :) The compiler is definitely doing something suspicious behind the courtain. Thanks.

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

                      I think you could do with studying all the features of C (or C++ if that is what you are using). Trying to learn a language by posting questions here will take ten times as long, and you will still miss most of it.

                      1 Reply Last reply
                      0
                      • C Calin Negru

                        struct TestStruct
                        {
                        int x;
                        int z;

                        };
                        TestStruct S;
                        TestStruct Funct()
                        {
                        return S;
                        }

                        //
                        Funct().x = 100;

                        Will this work?

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

                        Often I edit my posts/replies shortly after making them, I can`t organise myself in a single swing.

                        1 Reply Last reply
                        0
                        • C Calin Negru

                          the two dimensional array seems like something too good to be true :) The compiler is definitely doing something suspicious behind the courtain. Thanks.

                          _ Offline
                          _ Offline
                          _Superman_
                          wrote on last edited by
                          #14

                          Internally it's just continuous memory that is allocated for a 2 dimensional array (For any no. of dimensions for that matter). The compiler uses the specified dimensions to calculate the offset into the memory to fetch. For instance, offset of Chart[2][3] could be calculated as -

                          (sizeof(ChartNode) * MAXROW * 2) + (sizeof(ChartNode) * 3)

                          «_Superman_»  _I love work. It gives me something to do between weekends.

                          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                          Polymorphism in C

                          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