Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to define an dynamic int-array

Status
Not open for further replies.

mokesql

Programmer
Sep 6, 2001
30
0
0
AT
hi!

i wanna define a int-array which size should be typed in by the user. so i want to define the array dynamicly. i tried
cin << n;
int x[n][n];

but it doesnt work. i get the error: constatn expression required in the definition line of x. i once have had the same problem but i dont know how i solved it back then. please help me as soon as possible. thnx

moke
 
Uh, use alloc. if you're going to define an array, you have to do it at compile time. What you have to do is use a pointer to an array and use (m)alloc(...) to set it up at run time.

Mike.
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
You might also want to look the standard library vector and its cousins. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
First, sorry for my English...
You must to allocate memory using malloc (in C) or new in (C++). As you want to define a bidimensional array you could make:

int **i, n , k;

cin >> n;
i=(int **)new int [n];
for (k=0; k<n; k++)
i[k]=(int *)new int [n];

and when the program finish, you must free memory:

for (k=0; k<n; k++)
delete i[k];


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top