The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Size of numeric array (in C)
Discuss Size of numeric array (in C) in the C Programming forum on Dev Shed. Size of numeric array (in C) C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 14th, 2004, 08:45 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 34
Time spent in forums: 3 h 50 m 24 sec
Reputation Power: 10
|
|
|
Size of numeric array (in C)
Hiya!
To get the size of a char array, the command
strlen(chararray) does the job.
But is there a command that returns the size of
an int array?
Many thanks
|

February 14th, 2004, 08:51 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: Calgary, Alberta, Canada
Posts: 52
Time spent in forums: 2 h 58 m 35 sec
Reputation Power: 10
|
|
|
The sizeof() will tell you how much memory has been alloted to the array then you could divide by the sizeof(type) to determine how many elements can fit into that array. But to find out how many elements have been stored in that array.... I've never heard of a built-in function like say intlen() or something??
|

February 14th, 2004, 08:57 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 34
Time spent in forums: 3 h 50 m 24 sec
Reputation Power: 10
|
|
|
Yeah me neither...intlen() doesn't seem to exist. The sizeof() approach seems to be the best way...thanks!
|

February 14th, 2004, 09:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|
To get the size of a char array, the command
strlen(chararray) does the job.
No, the strlen() only returns the length of the string stored in the array. The size of the array could be a lot bigger than that.
But is there a command that returns the size of
an int array?
use sizeof operator, as other posters have mentioned.
|

February 15th, 2004, 12:29 AM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
#define intlen(x) ( sizeof(x) / sizeof(int) )
or better yet
#define alen(x, type) (sizeof(x) / sizeof(type) )
|

February 15th, 2004, 03:52 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
The following works too, with the added benefit of not having to supply the type.
Code:
#define ALEN(a) (sizeof(a) / sizeof((a)[0]))
Bear in mind that any of the macros presented in this thread thus far work only on arrays declared at compile time. If you use malloc() et al, all these macros are useless!
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

February 15th, 2004, 05:23 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 34
Time spent in forums: 3 h 50 m 24 sec
Reputation Power: 10
|
|
|
Cool! That answers all my questions!
Cheers
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|