LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-15-2003, 01:40 AM   #1
mojozoox
LQ Newbie
 
Registered: Jul 2003
Location: India
Distribution: RedHat 8
Posts: 9

Rep: Reputation: 0
Question Variable Array in C


For example i have this....

int h[]={10,20,30,40,50};

now is there a way to find out the length of this array?

pls help!
 
Old 12-15-2003, 02:33 AM   #2
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
[] is actualy much the same as having a pointer (int*) .. Theres no object or anything to do the housekeeping.. So. unless you terminate your array with a nice value its impossible to know the lenght.

You should teminate it with.. say a Zero.

int h[] = { 10, 20, 30, 40, 50, 0 };

Then you can find the lengths by searching for the Zero.
 
Old 12-15-2003, 04:35 AM   #3
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
yes , you can do it.

size = sizeof(h)/sizeof(int);

should give you the size of the array.

Hope this helps.

Regards,
 
Old 12-15-2003, 05:41 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Using sizeof() only works in the function in which the array is declared.
There are workarounds.

It wastes memory, but if you know the maximum possible size ahead of time for any array, dimension all arrays that size.
Code:
#define MAX_ARR_SIZE 256
....

int myarray[MAX_ARR_SIZE];

................
for (i=0;i<MAX_ARR_SIZE;i++) ....
 
Old 12-15-2003, 10:10 PM   #5
mojozoox
LQ Newbie
 
Registered: Jul 2003
Location: India
Distribution: RedHat 8
Posts: 9

Original Poster
Rep: Reputation: 0
Jeez thanx for all the suggestions but... as worldmagic suggested i guess there is no possible solution to finding the size in case i dont include a delimiter into array h!!
 
Old 12-15-2003, 10:54 PM   #6
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
If you want to get the size of the array in any function, you can use -


int * ptr = (int *)h;
int size ;
ptr--;
size = *ptr;

value of size will give you the size of the array.
 
Old 12-16-2003, 01:39 AM   #7
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Just have the function be called with an extra integer argument which will contain the size of the array. Otherwise, without using a terminating value there is no way of knowing the array size.
e.g:
Code:
void process_array (int array[], int size)
{
  int count;
  
  for (count = 0; count < size; count++)
   /* do some stuff */
}
 
  


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
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
calling a variable within a variable sdandeker Programming 9 04-28-2004 03:55 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM


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

Main Menu
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