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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use a function return value as array size

How to use a function return value as array size

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structurestutorialquestion
5 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.
  • J Offline
    J Offline
    Joschwenk666
    wrote on last edited by
    #1

    Hi, I try to initialize an array with a function return, but i get error C2057: expected constant expression. Is there an easy solution for the following problem?

    const int iRecordCountBefore = p_rs->GetRecordCount();
    int iRsPositionsUpdated[iRecordCountBefore] = {-1};

    Thanks you!

    N S 2 Replies Last reply
    0
    • J Joschwenk666

      Hi, I try to initialize an array with a function return, but i get error C2057: expected constant expression. Is there an easy solution for the following problem?

      const int iRecordCountBefore = p_rs->GetRecordCount();
      int iRsPositionsUpdated[iRecordCountBefore] = {-1};

      Thanks you!

      N Offline
      N Offline
      norish
      wrote on last edited by
      #2

      You cannot write such array code in C or C++ program syntax. You should change like below;

      const int iRecordCountBefore = p_rs->GetRecordCount();
      // if you use C++ syntax
      int* iRsPositionsUpdated = new int[iRecordCountBefore];
      // otherwise C syntax
      int* iRsPositionsUpdated = (int*)malloc(sizeof(int)*iRecordCountBefore);
      // if you want to initialize with -1 below dose
      memset(iRsPositionsUpdated, -1, sizeof(int)*iRecordCountBefore);

      J 1 Reply Last reply
      0
      • N norish

        You cannot write such array code in C or C++ program syntax. You should change like below;

        const int iRecordCountBefore = p_rs->GetRecordCount();
        // if you use C++ syntax
        int* iRsPositionsUpdated = new int[iRecordCountBefore];
        // otherwise C syntax
        int* iRsPositionsUpdated = (int*)malloc(sizeof(int)*iRecordCountBefore);
        // if you want to initialize with -1 below dose
        memset(iRsPositionsUpdated, -1, sizeof(int)*iRecordCountBefore);

        J Offline
        J Offline
        Joschwenk666
        wrote on last edited by
        #3

        Thank you for the working and fast answer!

        C 1 Reply Last reply
        0
        • J Joschwenk666

          Thank you for the working and fast answer!

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

          Don't forget to delete (if you used new) or free (if you used malloc) the memory when you don't need it anymore, otherwise it might lead to memory leaks.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • J Joschwenk666

            Hi, I try to initialize an array with a function return, but i get error C2057: expected constant expression. Is there an easy solution for the following problem?

            const int iRecordCountBefore = p_rs->GetRecordCount();
            int iRsPositionsUpdated[iRecordCountBefore] = {-1};

            Thanks you!

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            Can I suggest using a std::vector rather than an array, if only so you don't need to bother remember freeing memory? This will create a vector initialised with iRecordCountBefore integers, all set to -1.

            const int iRecordCountBefore = p_rs->GetRecordCount();
            std::vector<int> iRsPositionsUpdated(iRecordCountBefore, -1);

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            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