944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2053
  • C++ RSS
Mar 10th, 2005
0

array question

Expand Post »
I wanna create an array whose size would be determined by a variable, but i when i write the following, i got an error

int main ()
{
int a;
std::cin >> a;
int intArray [a];

return 0;
}

the error is "an constant expected".
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xfruan is offline Offline
12 posts
since Mar 2005
Mar 10th, 2005
0

Re: array question

The error is correct -- you can't do that in todays C++. Have you been introduced to new/delete?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 12th, 2005
0

Re: array question

you can't use a variable define a array's size
#include <iostream.h>
int main ()
{
int a;
cin >> a;
int *intArray;
intArray=new int(a);

return 0;
}
Reputation Points: 11
Solved Threads: 0
Newbie Poster
navyblue is offline Offline
6 posts
since Oct 2004
Mar 12th, 2005
0

Re: array question

Thanks for the answers, i can solve my problem now. But i got another question regarding arrays. I tried to make an array of c-style strings with the following code, but got an error:

C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. char ** strArray = new char [5][30];
  4. //using the array
  5.  
  6. delete [] strArray;
  7. return 0;
  8. }

the error message is: "the compiler is unable to convert from the type char (*) [30] to the type char **."

Why is it so and how can i solve it? :-|
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xfruan is offline Offline
12 posts
since Mar 2005
Mar 12th, 2005
0

Re: array question

Multidimensional arrays are actually arrays of arrays, so you need to allocate memory to reflect that:
C++ Syntax (Toggle Plain Text)
  1. char **p = new char*[rows];
  2.  
  3. for ( int i = 0; i < rows; i++ )
  4. p[i] = new char[cols];
Then to free them:
C++ Syntax (Toggle Plain Text)
  1. for ( int i = 0; i < rows; i++ )
  2. delete [] p[i];
  3. delete [] p;
The nice thing is that you can index an array allocated like this with the subscript operator:
C++ Syntax (Toggle Plain Text)
  1. cout<< p[i][j] <<endl;
Not all multidimensional allocation schemes allow you to do that.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: code problems
Next Thread in C++ Forum Timeline: c++ very new at





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC