C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2004, 08:45 PM
schorsche schorsche is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 34 schorsche User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 14th, 2004, 08:51 PM
gdjsmith gdjsmith is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Calgary, Alberta, Canada
Posts: 52 gdjsmith User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 58 m 35 sec
Reputation Power: 10
Send a message via MSN to gdjsmith
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??

Reply With Quote
  #3  
Old February 14th, 2004, 08:57 PM
schorsche schorsche is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 34 schorsche User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #4  
Old February 14th, 2004, 09:03 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
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.

Reply With Quote
  #5  
Old February 15th, 2004, 12:29 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
#define intlen(x) ( sizeof(x) / sizeof(int) )
or better yet
#define alen(x, type) (sizeof(x) / sizeof(type) )

Reply With Quote
  #6  
Old February 15th, 2004, 03:52 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
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/

Reply With Quote
  #7  
Old February 15th, 2004, 05:23 PM
schorsche schorsche is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 34 schorsche User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 50 m 24 sec
Reputation Power: 10
Cool! That answers all my questions!

Cheers

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Size of numeric array (in C)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap