Results 1 to 3 of 3

Thread: Array Problem - Please Help!!!

  1. #1

    Thread Starter
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409

    Unhappy

    What is wrong here.I want to set the array myarray depending on the value of the variable e2 and not on some specific number. Why doesn't this work.
    Code:
    int e2 = 15;
    char myarray[(int)e2];
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't allocate an array like that. You're allocating it on the stack, so you have to give a constant size. If you want to give it a dynamic size, you have to allocate it on the heap:
    Code:
    int e2 = 15;
    char *myarray = new char[e2];
    
    // ...use myarray
    
    delete myarray; // Very important!!!
    ...although don't try to use myarray after it's been deleted.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Thanks for the code it works just fine
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width