A Quick Question on C++ - Help Me~

Kenji4861

Banned
Jan 28, 2001
2,821
0
0
I want to read in the array size from the command prompt

int main(int argc, char* argv[]){

int array(argv[1]);
return 0;
}


That's giving me an error saying the array size.. is not constant.. any way to get around this?
 

bleeb

Lifer
Feb 3, 2000
10,868
0
0
YOU MORON... you're doing it all wrong.

First of all do you know how Main takes in the arguments???
 

bleeb

Lifer
Feb 3, 2000
10,868
0
0
If I remember correctly, it dynamically creates an array based on the size of whatever arguments you supply to main. Therefore, I'm not sure you can input an array size.
 

manly

Lifer
Jan 25, 2000
13,313
4,087
136
Originally posted by: bleeb
If I remember correctly, it dynamically creates an array based on the size of whatever arguments you supply to main. Therefore, I'm not sure you can input an array size.
Heh, great explanation for someone calling a beginner a moron. :p

First off, argv is an array of strings (more specifically, C strings, or char arrays). If argv[1] is the argument with your array size, then it first has to converted from a string to an int (use the library function sprintf).

Secondly in C/C++, arrays on the stack cannot be sized at runtime. Their size has to be known at compile time.

But an array on the heap can be sized at runtime. Like any other data on the heap, you'd access such an array through a pointer variable. Initialize the array with the new operator. For example:

int *someArray;
someArray = new int[variableSizeFromArgv];

Remember to properly delete the array later:

delete [] someArray;

I know I didn't give you exact source code from A to Z, but this is plenty of info to get you on the right track. Google is a great search engine and will often help you find answers to specific programming questions.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
If argv[1] is the argument with your array size, then it first has to converted from a string to an int (use the library function sprintf)



The function you probably meant was atoi(). From MSDN:

===================================================
atof, atoi, _atoi64, atol
Convert strings to double (atof), integer (atoi, _atoi64), or long (atol).

double atof( const char *string );

int atoi( const char *string );

__int64 _atoi64( const char *string );

long atol( const char *string );


 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
Originally posted by: Kenji4861
I want to read in the array size from the command prompt

int main(int argc, char* argv[]){

int array(argv[1]);
return 0;
}


That's giving me an error saying the array size.. is not constant.. any way to get around this?

Change it to....
int array[strlen(argv[1])];
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Change it to....
int array[strlen(argv[1])];


That's probably not what he wants. Here's probably what he does want:


#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc < 2)
return 0;

int array_size = atoi(argv[1]);
int* pArray = new int [array_size];


// ... use the array


delete[] pArray;
return 0;
}