LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-04-2004, 05:59 AM   #1
Hady
Member
 
Registered: Nov 2003
Posts: 55

Rep: Reputation: 15
Size of an array in C


How can I get the size of an array[] of integers in C?
 
Old 04-04-2004, 06:30 AM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Someone correct me if I'm wrong, but I don't think you can, unless you use some special value that marks the end of the array.
 
Old 04-04-2004, 06:37 AM   #3
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
if it is a static array you can: sizeof(array)/sizeof(int)
if it is dinamic you can't
 
Old 04-04-2004, 04:14 PM   #4
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Ah yes, forgot about sizeof(). And yes, it only works for statically allocated arrays.
 
Old 04-05-2004, 02:56 AM   #5
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
luxitan:
if it is a static array you can: sizeof(array)/sizeof(int)
if it is dinamic you can't

what you just suggested gives the number of elements in an array , and that too only if it is an array of int...

just the sizeof should do
 
Old 04-05-2004, 07:48 PM   #6
sharpie
Member
 
Registered: Jan 2004
Location: California
Distribution: Slackware 10.1
Posts: 190

Rep: Reputation: 30
Do you want the size in bytes, or the number of elements?

If you want the size in bytes, a simple:

Code:
sizeof array
will suffice. If you want the number of elements, use:

Code:
sizeof array / sizeof array[0]
The sizeof operator returns the size in bytes, so you're dividing the number of bytes in the whole array, by the number of bytes in the first element, thus you get the amount of elements.
 
Old 04-06-2004, 06:33 AM   #7
maseby
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 15
sizeof array / sizeof array[0]

Last edited by maseby; 04-06-2004 at 06:34 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++: array size limit? bigfatdud Programming 14 01-27-2005 12:19 PM
how to find out an array size on c++ poeta_boy Programming 9 06-22-2004 02:28 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
size of an array Longinus Programming 8 03-08-2004 02:48 PM
getting size of an array in c++ schatoor Programming 8 11-06-2003 06:19 PM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration