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. int array problem

int array problem

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
9 Posts 7 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.
  • P Offline
    P Offline
    prithaa
    wrote on last edited by
    #1

    Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa

    R M CPalliniC J 4 Replies Last reply
    0
    • P prithaa

      Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa

      R Offline
      R Offline
      Reagan Conservative
      wrote on last edited by
      #2

      Not sure I understand your problem, but the simplest thing to do is to initialize your array to zeros before you even use it. But why use an array of size 10 when a size of 4 would suit your needs??

      John P.

      1 Reply Last reply
      0
      • P prithaa

        Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        ints are just integer values. If you need to indicate an uninitialized int then you'll need to set aside one number to represent an invalid value. What are the valid range of values for your arry members? Pick a number outside that range :) Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        A 1 Reply Last reply
        0
        • M Mark Salsbery

          ints are just integer values. If you need to indicate an uninitialized int then you'll need to set aside one number to represent an invalid value. What are the valid range of values for your arry members? Pick a number outside that range :) Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          A Offline
          A Offline
          ajitatif angajetor
          wrote on last edited by
          #4

          Alternatively, you can use int pointers instead of array. This way you can check if the value is NULL

          B M 2 Replies Last reply
          0
          • A ajitatif angajetor

            Alternatively, you can use int pointers instead of array. This way you can check if the value is NULL

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #5

            or an array of int pointers.

            M 1 Reply Last reply
            0
            • A ajitatif angajetor

              Alternatively, you can use int pointers instead of array. This way you can check if the value is NULL

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Sure that'll work. Another alternative - make a "smart int" class, wrapping an int with some flag indicating its initialized status :) Then you wouldn't need the added indirection of a pointer. Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              1 Reply Last reply
              0
              • B bob16972

                or an array of int pointers.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                :-D To make sure, use an array of int pointer pointers, each member pointing to the corresponding member in the int pointer array, whose members point to the actual ints!

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                1 Reply Last reply
                0
                • P prithaa

                  Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  A dynamic array as std::vector<int> doesn't fit your needs?

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • P prithaa

                    Hello, I have an int array int a[10]. The array will have only 3 to 4 values in the it. so how do I check for NULL values in the array because I cannot check a[i]!=0 since I want 0 as a value Thanks Prithaa

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    You need to use a different value and initialize the array to that. Since you want to allow 0 to be placed in the array then use -1 to initialize it. That is fill the array with -1 and check for that. If that is not an option, and there is no other value you can use, then you have a real problem.

                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                    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