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. Please help me!

Please help me!

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorial
5 Posts 4 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.
  • D Offline
    D Offline
    dSolariuM
    wrote on last edited by
    #1

    Hi, I want to create run-time array for example I want to set the number of elements in run-Time: /**********************/ If i=0 then CClass cls[j]; else CClass cls[b]; /************************/ Best Wishes, Thanks.

    T R C 3 Replies Last reply
    0
    • D dSolariuM

      Hi, I want to create run-time array for example I want to set the number of elements in run-Time: /**********************/ If i=0 then CClass cls[j]; else CClass cls[b]; /************************/ Best Wishes, Thanks.

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      you will not be allowed to do this by the compiler. you'll have to allocate the memory for your array dynamically, on the heap :

      CClass cls* = new CClass[j];

      be sure that j is well defined, and non-negative ! another thing :

      If i=0 Then

      must be traduced in C/C++ by

      if (i == 0) {

      that means, be careful of the **( )** between the condition statement, and **==** for the equal operator (**=** is the affectation operator). cheers,


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      1 Reply Last reply
      0
      • D dSolariuM

        Hi, I want to create run-time array for example I want to set the number of elements in run-Time: /**********************/ If i=0 then CClass cls[j]; else CClass cls[b]; /************************/ Best Wishes, Thanks.

        R Offline
        R Offline
        Rahim Rattani
        wrote on last edited by
        #3

        Heres your solution CClass* cls; if (i==0) cls = new CClass[j]; else cls = new CClass[b]; Rahim Rattani Software Engineer, Matrix Systems (Pvt) Ltd., Karachi - Pakistan

        1 Reply Last reply
        0
        • D dSolariuM

          Hi, I want to create run-time array for example I want to set the number of elements in run-Time: /**********************/ If i=0 then CClass cls[j]; else CClass cls[b]; /************************/ Best Wishes, Thanks.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          And to complement the previous responses, don't forget to free the memory after having used it: if (cls) { delete[] cls; cls = NULL; }

          T 1 Reply Last reply
          0
          • C Cedric Moonen

            And to complement the previous responses, don't forget to free the memory after having used it: if (cls) { delete[] cls; cls = NULL; }

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            yes, of course ! :-D and to be more better, you can encapsulate this code into the destructor ~CClass()...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            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