View previous topic :: View next topic |
Author |
Message |
gopalakrishnan
Joined: 18 Jun 2010 Posts: 25
|
i am having doubt in sizeof(arrayname[])/sizeof(datatype) |
Posted: Wed Aug 25, 2010 7:26 am |
|
|
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: 19540
|
|
Posted: Wed Aug 25, 2010 8:42 am |
|
|
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
|
|
Posted: Wed Aug 25, 2010 12:04 pm |
|
|
I use this macro:
#define nElements(array) (sizeof(array) / sizeof(array[0]))
Ken |
|
|
gopalakrishnan
Joined: 18 Jun 2010 Posts: 25
|
hai regarding macro |
Posted: Thu Aug 26, 2010 10:35 pm |
|
|
what is macro?
#define nElements(array) (sizeof(array) / sizeof(array[0]))
what it mean? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Aug 26, 2010 11:57 pm |
|
|
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. |
|
|
|