Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Student Developer with Array Problems

Student Developer with Array Problems
Thread Tools
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Sep 22, 2002, 07:39 PM
 
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
Reply With Quote
Sep 22, 2002, 07:51 PM
 
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
Reply With Quote
Sep 22, 2002, 07:57 PM
 
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
Reply With Quote
Sep 22, 2002, 09:40 PM
 
... 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.
     
nyr44  (op)
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Sep 22, 2002, 10:59 PM
 
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
     
nyr44  (op)
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Sep 22, 2002, 11:07 PM
 
Never mind:

http://leepoint.net/notes/cpp/newdel...ynexample.html

Please excuse my stupidity. Thank you for your help. You put me on the right track!!!!

~Kris
     
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Sep 23, 2002, 09:13 AM
 
Originally posted by nyr44:
Never mind:

http://leepoint.net/notes/cpp/newdel...ynexample.html

Please excuse my stupidity. Thank you for your help. You put me on the right track!!!!

~Kris
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:49 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2