Easy question!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shisou
    New Member
    • Jul 2007
    • 52

    Easy question!

    Hey guys,

    This should be easy but I need a little help.

    This is in C... I have an array or ints I need to put something at the end that I can find to stop a loop... I tried using '\0' or setting it to NULL, but those seem to translate to 0... which doesn't work for me.

    any help is appreciated!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    An easy solution is to use a dummy value like -9999, but that may not be a very satisfactory solution for you.

    Why do you have an integer-filled array with a size larger than the actual number of elements in it?

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Originally posted by Shisou
      I tried using '\0' or setting it to NULL
      NULL is 0 and '\0' is the null character, with ASCII code 0. Anything you choose as a sentinel had better not appear in the array or you'll get premature termination. Perhaps INT_MIN, defined in <limits.h>, if you're working with signed ints, or INT_MAX if you aren't? (If you don't know, you're using signed).

      Comment

      • Shisou
        New Member
        • Jul 2007
        • 52

        #4
        hmm... i think this is going to be too complicated to find an easy solution to... and i have one that isn't perfect but it's close enough...

        Thanks anyways guys... unfortunately it seems a work around is the best i can do

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Alternatively you can store the index position of the element in the array that
          represents the logical end of the array. Of course you can't store that index
          position in the array itself.

          kind regards,

          Jos

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You should not really need to use a sentinel value to stop a loop iterating through an array of ints.

            Either it is a true array in which case you can get the endpoint using the sizeof operator.

            Or it is a pointer, however at some point you knew the size of the array, either it was a true array and you have passed a pointer to it to another function or it was malloced some time in which case at the time you malloced it you new its size.

            It you are passing a pointer to an array around functions then pass its size around too.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Read this article: http://bytes.com/forum/thread772412.html

              Arrays as a rule do not have sentinal values. You have to separately know the number of elements in the array.

              All that \0 does is make a char array useable in a special context. I suppose you could do the same with your int array if you chose a value that is outside the range of your data.

              Comment

              Working...