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

length of float array

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



Joined: 06 Oct 2008
Posts: 43

View user's profile Send private message

length of float array
PostPosted: Tue Mar 27, 2012 4:20 am     Reply with quote

hi,
if i have a float array
Code:

     float arr[20] = {};
 


and during the course of my process, this array is being filled up with values. how can i find out the length of my array that is being used. i mean, is there like a function like strlen but will work with float arrays?

please help
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 4:32 am     Reply with quote

No.
Strlen only 'works', because a 'string' in C is 'null terminated'. So the routine can look through the characters till it finds this 'null', and know how long the string is.
Assuming that you value can never have some specific unused byte value (for example all four bytes=0xff - which is an unused pattern in both IEEE float, and CCS float), then you could generate a 'strlen' equivalent, by filling the array with this value, and testing where it is in the array to find the number of locations used - remember though that just as with a 'string', this would then require the array to be one number _larger_, so there is always room for the extra terminator.
However I have to point out, that if you are filling the array, then you must have a counter saying where you are writing the data, and this in itself tells you how many values are 'in use'.....

Best Wishes
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 6:17 am     Reply with quote

In C strings are a special case of character arrays as Ttelmah points out. In C all arrays are fixed length. They don't change size. How much data you put into them is entirely your business, and for you to keep track of.

Maybe you're used to C# which has flexible collections. This built on C++'s standard library which includes collection-like objects. These simply don't exist in C. That's a good thing, as otherwise the memory overhead would be so overwhelming that almost none of our code would fit in a PIC.

RF Developer
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Mar 27, 2012 6:45 am     Reply with quote

for one of my projects i had a buffer that got filled with a string comming in through serial.
the buffer contained commands to be executed, and they varied in length.

so what i did was prior to listening for an incomming command i would initialize my buffer to all NULL:

Code:
Buffer = {'\0','\0','\0','\0','\0','\0','\0','\0','\0'} //not actual code.

my serial ISR would strart filling up the buffer from Buffer[0];.

so i ended up with somthing like:

Code:
Buffer = {'f','o','o','b','a','r','\0','\0','\0'} //not actual code



from then on, i could do what ever string manipulations i needed.
in my case, STRSTR().


you can also have a string as such:
Code:
Buffer = {'f','o','o','b','a','r','1','\0','f','o','o','b','a','r','2','\0','f','o','o','b','a','r','3','\0'} //not actual code



do comparisons and searches starting from other possitions not '0'.

maybe that helps you.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
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