Thread: Empty Array

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35

    Empty Array

    Can I ask how to determine if an element of an array has not been used/empty or not initialized. For example, I have 150 array elements, and I dont know how many array elements will be filled. So I need to check which array element is not used. How do I do that? or what function I have to use? I know that an array is not empty, but I'm confused if its, \0 or \null? And can I use strcmp() to compare those?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You can't. Accessing an uninitialised value gives undefined behaviour, so there is no way of reliably detecting an uninitalised value.

    You need to do your bookkeeping. For example, keep track (in a separate variable) of how many elements you have placed in the array. Or use a sentinel to mark one past the last value. The standard string functions such as strcmp(), use a sentinel of '\0'. Either way, you need to make sure you never access past the end of the array (if the array is 150 characters, never read or write array[x] where x is 150 or more, or x is less than zero).

    '\0' is a character with value 0.
    Last edited by grumpy; 09-03-2011 at 04:06 AM. Reason: fix typo
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    thanks grumpy, at least I have an idea to know which array is emtpy.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by edmund085 View Post
    thanks grumpy, at least I have an idea to know which array is emtpy.
    A trick you can use is when creating your array, fill it with impossible values...
    Lets say your code never works with negative numbers... simply fill the entire array with -1, as you fill it it will be replaced by your working values. To discover the first empty element simply loop through it searching for -1.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    To make it perfectly clear, there is no concept of an empty array. "empty" is just your own convention.
    E.g. If you have an array of int[50] then you always have 50 ints, no matter what. You could use zero to indicate that a certain index does not contain a useable value, or -1 as suggeted, or even 42 if you wanted. Of course it depends on the type of the array.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    You could use zero to indicate that a certain index does not contain a useable value, or -1 as suggeted, or even 42 if you wanted. Of course it depends on the type of the array.
    Just to point out: such values are what I referred to as a sentinel in my post. The thing with sentinels is that it is necessary to reserve some value (or sometimes a set of values, such as negative values) to be use as a sentinel. That is problematical if all possible values have meaning for your program.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to tell if an array is empty in a structure?
    By lurikeen in forum C Programming
    Replies: 4
    Last Post: 04-02-2009, 09:55 PM
  2. Empty Array?
    By Terran in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 12:47 AM
  3. Empty an Array
    By Charlin in forum C++ Programming
    Replies: 11
    Last Post: 01-12-2008, 02:48 PM
  4. Empty Array
    By devilsknight in forum C Programming
    Replies: 16
    Last Post: 07-18-2006, 09:52 PM
  5. making an empty 2-D array
    By starX in forum C Programming
    Replies: 4
    Last Post: 02-08-2002, 01:09 AM