array question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 12
Reputation: xfruan is an unknown quantity at this point 
Solved Threads: 0
xfruan xfruan is offline Offline
Newbie Poster

array question

 
0
  #1
Mar 10th, 2005
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".
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: array question

 
0
  #2
Mar 10th, 2005
The error is correct -- you can't do that in todays C++. Have you been introduced to new/delete?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 6
Reputation: navyblue is an unknown quantity at this point 
Solved Threads: 0
navyblue navyblue is offline Offline
Newbie Poster

Re: array question

 
0
  #3
Mar 12th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 12
Reputation: xfruan is an unknown quantity at this point 
Solved Threads: 0
xfruan xfruan is offline Offline
Newbie Poster

Re: array question

 
0
  #4
Mar 12th, 2005
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:

  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? :-|
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: array question

 
0
  #5
Mar 12th, 2005
Multidimensional arrays are actually arrays of arrays, so you need to allocate memory to reflect that:
  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:
  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:
  1. cout<< p[i][j] <<endl;
Not all multidimensional allocation schemes allow you to do that.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC