 |
 |
Student Developer with Array Problems
|
 |
|
 |
|
Junior Member
Join Date: Sep 2000
Status:
Offline
|
|
Hello all. This is my first post in a developers forum, so please be gentle.
I am using Codewarrior v8.2, and I am trying to do a simple array sort. I am using the following line of code:
//First, get the number of integers in the
//array
int n=0;
cout<<"Enter the total number of integers to be placed in the array:";
cin>>n;
//Next, create and fill the array
int a[(n-1)]; //Subtract 1 to account for
//the 0 index
I am getting:
Error : illegal constant expression
a2.cpp line 32 int a[(n-1)]; //Subtract 1 to account for the 0 index
Project: array sort, Target: ANSI C++ Console Carbon, Source File: a2.cpp
Is my code wrong, or has Codewarrior gone crazy? I checked several refrences, and cannot find another reason than a compiler bug. Has anyone else seen this? Does anyone have any ideas?
Thanks for your help.
~Kris
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
If you want 10 items in an array of integers, it should be declared as:
int arrayOfInts[10];
But its maximum index would still be 9.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
Your code IS "wrong." The theory is close to correct. The first question is whether you are doing this in C or C++. This will determine what the "correct" solution is. I do see from the compile error, though, that you are doing C++. (note that Objective-C, and thus Cocoa, would use the standard C method)
The problem is that the size of the array is set by the compiler. It's statically allocated when you define it in the sense of:
int array[<insert number here>];
Thus, when you put a variable in there, it gets all messed up and can't compile it. What you have to do is set up the code so that the array is allocated at run time.
in c++:
int array[] = new int[n];
Then, when you are done with the array:
delete array[];
That's the compile error. There's also a logic error. When you allocate the array, you put the total number of values that you want... you do NOT account for the fact that indexing starts at zero--because that's irrelevant when you are telling it how big you want. So, the line I wrote up above would be the correct way.
In C, allocating and deallocating memory is done differently. I've done my best to avoid C, so off the top of my head, I'd probably give you really screwed up information. However, the concept is similar in that it must be done at run time.
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 2001
Status:
Offline
|
|
... in case you were wondering, in straight C you would use the calloc() function to dynamically allocate an array... but the above for C++ is nice and easy.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Sep 2000
Status:
Offline
|
|
Well, I tried what was posted, and got these errors:
Error : illegal initialization
a2-1.cpp line 79 int a[] = new int[n];
Error : expression syntax error
a2-1.cpp line 100 delete a[];
1.) Is there anything else I am forgetting?
2.) Is this in all compilers?
3.) Does this get any easier?
4.) Can you direct me to a web page or book that lists this information? It's not a matter of trust, but if I can't document where I got this information, I will be kicked out of school on a charge of plagiarism. I am not kidding.
Thank you all yet again!!
~Kris
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Sep 2000
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
Rats... thought I had it right. Sorry. Oh well; at least you figured it out from that--that's even more important ('cause you are thinking).
Glad we could help!
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|