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 Initialize an array of user selected size

How to Initialize an array of user selected size

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestiondata-structureshelp
2 Posts 2 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.
  • D Offline
    D Offline
    Deepak Samuel
    wrote on last edited by
    #1

    Hi, How do I initilaize an array whose size will be determined by an user input(For example from a edit box ? I tried this but they didnt work... numsamples = m_numsamp; const long arr = numsamples; ULONG x[arr]; ULONG y[arr]; They returned error 2057 and 2466. Thanks! Deepak Samuel.

    R 1 Reply Last reply
    0
    • D Deepak Samuel

      Hi, How do I initilaize an array whose size will be determined by an user input(For example from a edit box ? I tried this but they didnt work... numsamples = m_numsamp; const long arr = numsamples; ULONG x[arr]; ULONG y[arr]; They returned error 2057 and 2466. Thanks! Deepak Samuel.

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      You need to dynamically allocate your array. This is where pointers comes very handy!

      ULONG* arrA = NULL;
      int nSize;
      
      // Get size of array from user
      ..
      // allocate array to given size
      arrA = new ULONG[nSize];
      if(NULL != arrA)
      {
         // work with array.
         arrA[0] = 1234;
         cout << _T("yeah baby!") << arrA[0] << endl;
      
         // don't forget to clean up when finished
         delete [] arrA;
      {
      

      I Dream of Absolute Zero

      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