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 CCS Technical Support

i am having doubt in sizeof(arrayname[])/sizeof(datatype)

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



Joined: 18 Jun 2010
Posts: 25

View user's profile Send private message

i am having doubt in sizeof(arrayname[])/sizeof(datatype)
PostPosted: Wed Aug 25, 2010 7:26 am     Reply with quote

I am using ccs c 4.093 version compiler. Does this compiler support
sizeof(arrayname)/sizeof(datatype);
This is the syntax to determine the size of an array. I used this above syntax in ccs to determine the array size. I am getting wrong array size.

If above mentioned ccs version compiler support these function, then is there any other function to determine the size of an array ?

Thanks in advance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Wed Aug 25, 2010 8:42 am     Reply with quote

What are you actually seeing 'wrong'?. Make sure there is not something silly happening, like the data type you are putting the result into being too small.
Just tried a really basic test with your compiler version:
Code:

#include "C:\Program Files\PICC\USBtest\test4680.h"
float test[300];

void main()
{
   int16 length;

   length=sizeof(test)/sizeof(float);
   printf("%3lu/n/r",length;
   
   while(true) ;
}

With just basic serial setups, aqnd fuses in the 'test4680' include, and it happily prints "300".

Best Wishes
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Wed Aug 25, 2010 12:04 pm     Reply with quote

I use this macro:

#define nElements(array) (sizeof(array) / sizeof(array[0]))

Ken
gopalakrishnan



Joined: 18 Jun 2010
Posts: 25

View user's profile Send private message

hai regarding macro
PostPosted: Thu Aug 26, 2010 10:35 pm     Reply with quote

what is macro?
#define nElements(array) (sizeof(array) / sizeof(array[0]))
what it mean?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 26, 2010 11:57 pm     Reply with quote

A macro is a standard command from C. Look this up into your C language book. If that doesn't make it clear to you, then use the internet to find some more explanations. Sorry, I don't to sound unhelpful, but a teaching book can explain this much better than I can do in a couple of lines.

What the given macro does is the same thing your code does, but instead of specifying the data type it uses the size of the first element in the array. Just a clever way of removing one variable to be specified.
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