CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to check if array is full

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tandy
Guest







How to check if array is full
PostPosted: Tue Jun 17, 2008 5:05 pm     Reply with quote

Hi,

I am a beginner,

How do I check if my array is full or not?

Code:


int array[96];

//then i put some data onto it

if( condition) // to check if the array is full or not

{}




It would be highly appreciated if someone could help me do this.

Thank You

Tandy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 18, 2008 12:06 am     Reply with quote

Normally you know this, because you (the programmer) are incrementing
an index into the array. The number of elements is the same as the
current value index.

Or, if you use strcpy() to copy a string into the array, you can use
strlen() to find the length of the string. Though, ideally, you would check
the length before you copy it into the array, to make sure that you don't
write beyond the end of the array.

If this doesn't answer your question, then you need to give more details
about how you are declaring the array and how you are writing to it.
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Wed Jun 18, 2008 9:18 am     Reply with quote

Just to clear things up a little (or confuse you more..)
There's no such thing as an "empty" (or half-full) array.... Your array is always "full'. When you declare it, it is already full of garbage bytes (or zeroed out if you have cleared the memory)
As PCM Programmer says you can however keep track of what part of the data useful to you in the array, and what are the locations that contain useless (not more needed) 'garbage' data (you can call it empty space) that you can use for storing new data.
It is up to your code to do it.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Jun 18, 2008 11:34 am     Reply with quote

Are we to assume that the array index gets incremented every time you load an element?

One way to check for array-full, or at least get somewhat close to doing it, is to work with a defined value for the array size and then check for it:

Code:

#define ARRAY_SIZE  96

  int array[ARRAY_SIZE];

  if(index >= ARRAY_SIZE) // to check if the array is full or not
  {                              // Handle the full condition
  }
                                // OK, then i put some data onto it
  array[index] = my_new_data;
  index++;                // Increment, ready for next time



Note "defensive programming" use of the >= compare. You wouldn't want a situation where the index went above 96 for some reason and you were unable to detect it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group