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. Arrays!

Arrays!

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
8 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.
  • C Offline
    C Offline
    CreepingFeature
    wrote on last edited by
    #1

    Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.

    A J V T D 5 Replies Last reply
    0
    • C CreepingFeature

      Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.

      A Offline
      A Offline
      Antony M Kancidrowski
      wrote on last edited by
      #2

      If you don't define all elements of the array then they will be undetermined and can be anything. It will be pure chance that they are 0. If you want the array elements to default to NULL you need to NULL the array first. Ant. I'm hard, yet soft.
      I'm coloured, yet clear.
      I'm fruity and sweet.
      I'm jelly, what am I? Muse on it further, I shall return!
      - David Williams (Little Britain)

      1 Reply Last reply
      0
      • C CreepingFeature

        Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.

        J Offline
        J Offline
        Jaime Stuardo
        wrote on last edited by
        #3

        First of all, you cannot trust on compiler about filling all assigned memory with 0's. In VC++, it is initialized with garbage, so be sure to call a function to initialize array, for example, ZeroMemory or memset. Now to your question. If you declare char array[] you have an array of bytes, so you have to compare against 0, not '0' (comparing with '0' is like comparing with 0x30) Jaime

        P 1 Reply Last reply
        0
        • C CreepingFeature

          Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          Also note that NULL and 0, '0' are NOT the same. 0 = a value NULL is nothing, nada, ... "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

          T 1 Reply Last reply
          0
          • V V 0

            Also note that NULL and 0, '0' are NOT the same. 0 = a value NULL is nothing, nada, ... "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            NULL is often defined as 0 or 0L. It is implementation specific. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            1 Reply Last reply
            0
            • C CreepingFeature

              Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              The only time the value is required to be zero is if the array is a global variable. Otherwise the value can't be predicted. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              1 Reply Last reply
              0
              • J Jaime Stuardo

                First of all, you cannot trust on compiler about filling all assigned memory with 0's. In VC++, it is initialized with garbage, so be sure to call a function to initialize array, for example, ZeroMemory or memset. Now to your question. If you declare char array[] you have an array of bytes, so you have to compare against 0, not '0' (comparing with '0' is like comparing with 0x30) Jaime

                P Offline
                P Offline
                Per Nilsson
                wrote on last edited by
                #7

                Well. When an array is declared, its content is undefined unless you give it an initial value. Initial values are assigned to an array by listing values withing {}. If your array is larger than your initialization list, the remaining elements will be assigned 0 by the compiler. int a[4]; // Undefined value int b[4] = {1,2}; // Contains values 1, 2, 0, 0 /Per

                1 Reply Last reply
                0
                • C CreepingFeature

                  Quick DUMB :omg: question. I know that an (int array[]) which was not given values for all elements fills them with zeroes. Say an int array[3] = {1, 4} would have array[0] = 1, array[1] = 4; and array[2] = 0 (that is assigned by compiler). A think thats how it is :confused:! Now if I got a (char array[]) is the empty element given a integer 0 or some char. And if I was to check for it would I use simple if (... == 0) or if (... == '0'). Thanks in advance.

                  D Offline
                  D Offline
                  digwizfox
                  wrote on last edited by
                  #8

                  Also, not mentioned before, is that static arrays are always initialized with zero for each value. I wouldn't recommend making a variable static for that reason alone, but it is interesting to remember that for future reference. If you have an array that is in a global function and you don't want it to be reallocated on the stack every time the function is called, you could make an array static. The values are all zero the first time the function executes and after that, the values will stay in memory. The initialization only takes place once, even though the function call could be made many times.

                  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