Thread: Dynamic arrays

  1. #1
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    Dynamic arrays

    Code:
     int** map = new int*[10]; 
    for(int i=0;i<10;i++){
        map[i] = new int[10];
    }
    Now why doesn't this work for me?
    I've looked around everywhere, and im trying to create arrays of dynamic size. My sources tell me it should work, but i get a "parse error before 'for'."

    Yet i'm not sure what is wrong there.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    why not ty linked lists or vectors? I dont think arrays were ever ment to/able to be dynamic

  3. #3
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    I got it to work by itself now, so i know its fine
    Its something in the rest of my code thats wrong.

    But all that is above this is a bunch of #defines.

    I moved the code to directly under the #includes, and it still has the same problem :S
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  4. #4
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Code:
    #include <string> 
    #include <string.h>
    #include <fstream> 
    #include "allegro.h"
    using namespace std;
    
    int** map = new int*[40]; 
    for(int i=0;i<40;i++)
    {
        map[i] = new int[40];
    }
    It has a parse error there somewhere (same error still), before "for"
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Is this all your code? Where is your main?

  6. #6
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    No it wasnt all my code. But i narrowed the error to this:

    Code:
     int** ptr = new int*[10];
    for(int i=0;i<10;++i){		
        ptr[i] = new int[10];	
    }
    
    int main(){	
    
    ptr[0][1] = 12313; 
    return 0;
    }
    Its not in the main function, but if i put it in there, it works. Is it illegal to try and create the array like this outside of a function?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  7. #7
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Ok, well then how would i go about creating this as a global array?

    Would vectors work?

    :EDIT: Tell me im stupid for forgetting that pointers are 'global'
    Last edited by fry; 09-11-2002 at 05:03 AM.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM