C++ illegal arrays of references

Discussion in 'Software' started by quirk, Nov 23, 2005.

  1. quirk

    quirk Corporal

    I'm sorry if this is really basic, but it's not in my book and
    I didn't find a thread already touching on this........
    I'm trying to initialize an array with a variable as its size,
    but when I try to build, it gives me errors.
    I've seen the professor use global constants for sizes before,
    but not user defined variables.
    example-
    string name [records];
    cout enter #of records;
    cin records
    and then a loop to input the name
    Any help would be greatly appreciated.
     
  2. QuickSilver

    QuickSilver Corporal

    I don't believe its possible to create an array of a dynamic size like that in C or C++... I hope someone will correct me if I'm wrong but my understanding of it is because the program won't know how much memory to allocate to the array because its size is variable...

    So its not possible to create an array of dynamic size?

    Yes it is, but you have to do it differently. I believe this is much easier in C++ but it works by dynamically allocating memory from the heap. I seem to recall that if you wanted an array of objects you would use the 'new()' constructor but its been many years since I did this so I can't remember offhand - it also looks like this isn't really the solution you want.

    So, if I were you I would just use a #define or global constant like your tutor currently does, and as you progress you'll get proper guidance on using the heap etc.

    Hope that helps...
     
  3. quirk

    quirk Corporal

    thanks for the prompt help!
    I actually used a work around to bypass the problem, but would still love
    to know of a proper way to do it if possible.

    c++ allows you to write over the array limit (on most comps ;) ) and leave
    empty elements, so it just amounts to buggy, sloppy code. I just want to
    know the right way to do everything so I don't look like an idiot in a year
    or two on the job.
    thanks again.
    drew
     
  4. snakefoot

    snakefoot Sergeant Major

    Maybe if you allocated the array after receiving the number of records using new[], then it would work.

    Instead of using c-arrays then one should consider using STL that provides generic containers that can be dynamic in size:
    Code:
    using namespace std;
    
    list<string> strs;
    cout << "Enter some strings, (enter, ctrl-z, enter to quit): ";
    istream_iterator<string> input(cin);
    
    while (!cin.eof())
       strs.push_back(*input++);
    What Is an Iterator in C++, Part 2
     
  5. quirk

    quirk Corporal

    thanks.
    I added a bookmark so that I could reference that after next semester.
    Hopefully in a month or two it won't be so high above my head :)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds