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 pass size of an array from command line

How to pass size of an array from command line

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorial
6 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Is there any way to pass the size of an array using a command prompt.

    #include using namespace std;

    int main(int argc, char *argv[])
    {
    //const int SIZE = 5;

    int arr\[ argv\[1\] \];
    
    return 0;
    

    }

    R 1 Reply Last reply
    0
    • L Lost User

      Is there any way to pass the size of an array using a command prompt.

      #include using namespace std;

      int main(int argc, char *argv[])
      {
      //const int SIZE = 5;

      int arr\[ argv\[1\] \];
      
      return 0;
      

      }

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      Arguments come as an array of char * - that's the argv variable. One way to convert them is :

      int size = atoi( argv[1] );

      L 1 Reply Last reply
      0
      • R Rick York

        Arguments come as an array of char * - that's the argv variable. One way to convert them is :

        int size = atoi( argv[1] );

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

        Now consider this code

        #include

        using namespace std;

        class cls
        {
        private:
        int arr[len];

        public:
        	int len;
        	void print\_arr();
        

        };

        int main(int argc, char *argv[])
        {
        cls obj;
        obj.len = atoi( argv[1] );
        obj.print_arr();

        return 0;
        

        }

        void cls::print_arr()
        {
        for (int i = 0; i < len; i++)
        {
        arr[i] = i;
        cout << arr[i] << " ";
        }
        }

        Getting errors error: ‘len’ was not declared in this scope error: ‘arr’ was not declared in this scope

        R CPalliniC 2 Replies Last reply
        0
        • L Lost User

          Now consider this code

          #include

          using namespace std;

          class cls
          {
          private:
          int arr[len];

          public:
          	int len;
          	void print\_arr();
          

          };

          int main(int argc, char *argv[])
          {
          cls obj;
          obj.len = atoi( argv[1] );
          obj.print_arr();

          return 0;
          

          }

          void cls::print_arr()
          {
          for (int i = 0; i < len; i++)
          {
          arr[i] = i;
          cout << arr[i] << " ";
          }
          }

          Getting errors error: ‘len’ was not declared in this scope error: ‘arr’ was not declared in this scope

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          The array needs to be dynamically allocated. This is done using the new operator. I usually prepend an "m_" to member variable names so I know that is what they are. The array will be allocated in the class constructor and released in the destructor.

          class cls
          {
          private:
          int * m_array;

          public:
          int m_length;

          cls( int length );   // constructor
          ~cls();              // destructor
          void print\_array();
          

          };

          cls::cls( int length ) // constructor
          {
          m_length = length;
          m_array = new int[length];
          }

          cls::~cls() // destructor
          {
          delete [] m_array;
          m_array = nullptr;
          }

          int main(int argc, char *argv[])
          {
          int length = atoi( argv[1] );
          cls obj( length );
          obj.print_array();
          return 0;
          }

          L 1 Reply Last reply
          0
          • R Rick York

            The array needs to be dynamically allocated. This is done using the new operator. I usually prepend an "m_" to member variable names so I know that is what they are. The array will be allocated in the class constructor and released in the destructor.

            class cls
            {
            private:
            int * m_array;

            public:
            int m_length;

            cls( int length );   // constructor
            ~cls();              // destructor
            void print\_array();
            

            };

            cls::cls( int length ) // constructor
            {
            m_length = length;
            m_array = new int[length];
            }

            cls::~cls() // destructor
            {
            delete [] m_array;
            m_array = nullptr;
            }

            int main(int argc, char *argv[])
            {
            int length = atoi( argv[1] );
            cls obj( length );
            obj.print_array();
            return 0;
            }

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

            Thanks :)

            1 Reply Last reply
            0
            • L Lost User

              Now consider this code

              #include

              using namespace std;

              class cls
              {
              private:
              int arr[len];

              public:
              	int len;
              	void print\_arr();
              

              };

              int main(int argc, char *argv[])
              {
              cls obj;
              obj.len = atoi( argv[1] );
              obj.print_arr();

              return 0;
              

              }

              void cls::print_arr()
              {
              for (int i = 0; i < len; i++)
              {
              arr[i] = i;
              cout << arr[i] << " ";
              }
              }

              Getting errors error: ‘len’ was not declared in this scope error: ‘arr’ was not declared in this scope

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Consider also using a vector[^], instead of a C-like array.

              In testa che avete, signor di Ceprano?

              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