Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamic array

Dynamic array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hanno25
    wrote on last edited by
    #1

    Hi! Do somebody know, how to create an dynamical array? I tried to do something like that: int count = 0; while (s == newName) { int i = atoi(s); count ++; int arr[count]; cin >> arr[i]; } As long, as it founds some elements in a list, it should size the length of the array and fill it. Thanks -- modified at 2:41 Saturday 1st April, 2006

    J S 2 Replies Last reply
    0
    • H hanno25

      Hi! Do somebody know, how to create an dynamical array? I tried to do something like that: int count = 0; while (s == newName) { int i = atoi(s); count ++; int arr[count]; cin >> arr[i]; } As long, as it founds some elements in a list, it should size the length of the array and fill it. Thanks -- modified at 2:41 Saturday 1st April, 2006

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #2

      Replace these lines: int arr[count]; cin >> arr[i]; With this: int* pArray = new int[count]; cin >> pArray[i] Then after you are done using the array: delete [] pArray; John -- modified at 3:01 Saturday 1st April, 2006

      1 Reply Last reply
      0
      • H hanno25

        Hi! Do somebody know, how to create an dynamical array? I tried to do something like that: int count = 0; while (s == newName) { int i = atoi(s); count ++; int arr[count]; cin >> arr[i]; } As long, as it founds some elements in a list, it should size the length of the array and fill it. Thanks -- modified at 2:41 Saturday 1st April, 2006

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        First include this: #include <vector> Now we'll make a typedef for a vector (essentially dynamic array) of ints. typedef std::vector<int> IntArray; Now he's your code rewritten to use the vector: int count = 0; IntArray arr; while (s == newName) {      int i = atoi(s);      coun++;      int num;      cin >> num;      arr.push_back(num); } This code look like it would get in an infinite loop to me - But that's another story. Steve

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups