• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

A Quick Question on C++ - Help Me~

Kenji4861

Banned
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?
 
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.
 
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. 😛

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.
 
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 );


 
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])];
 
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;
}
 
Back
Top