Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Dynamically allocated array using command line arguments

I have to implement a Union/Find tree in C++. The size of my parent array is to be generic, and I am supposed to pass in the size of the array on the command line, (argv[]). I have my Union/Find tree in a seperate class, and I am thinking this somehow needs to be done in the constructor.
My question is : How do I dynamically allocate an array of the size specified by the user in the command line?

Thanks in advance for any help
0
CPLUSPLUSRookie
Asked:
CPLUSPLUSRookie
1 Solution
 
christopher_smithCommented:
maybe like this...

#include <stdlib.h>

void main(int argc, char *argv[ ])
{
     int intSize = 0;
     int *intDynArray;
     if (argc > 1)
     {

          intSize = atoi(argv[1]);
                //check it is greater than 0 first...
          intDynArray = new int[intSize];

          delete [] intDynArray;
     }
     
}

regards

chris
0
 
CleanupPingCommented:
CPLUSPLUSRookie:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now