Hii friends,
Is it possible to find the no. of elements in an array after inserting without counting while insertion

Recommended Answers

All 23 Replies

you mean something like this sizeof(array)/sizeof(type of array);

>>Is it possible to find the no. of elements in an array after inserting without counting while insertion

depends. For statically allocated arrays see andor's response. Otherwise, it is not possible because c arrays do not keep that kind of information.

If you are using C arrays and the array is dynamuically allocated then you should have an integer that contains the current size of C arrays.

So basically, no. All sizeof does is give you the size of the array, not the number of elements entered. You have to count as you insert.

if we have a string array , we can easily use strlen() and get the array length, but what if it is an integer array ?
how can i find the no of elements in an int array ?
ex:
if i allocate space like this
int no[20];
and i store about 10 nos in the array, how do i check the no of elements on the array ?
because when i use sizeof(no)/ sizeof(no[0]) it gives me 20 . thats not what i want

i would appriciate if somebody can help me

>how do i check the no of elements on the array ?
Strings take an character value (the '\0' character) and use it as a sentinel. You can do the same thing with an integer array, but just like with strings, you can't use that sentinel as a legitimate value. If you don't want to go that route, you're basically stuck with maintaining a separate count.

if we have a string array , we can easily use strlen() and get the array length, but what if it is an integer array ?
how can i find the no of elements in an int array ?
ex:
if i allocate space like this
int no[20];
and i store about 10 nos in the array, how do i check the no of elements on the array ?
because when i use sizeof(no)/ sizeof(no[0]) it gives me 20 . thats not what i want

i would appriciate if somebody can help me

Hi...i am prabhakar......i am using an string array with 100 length..but i am inserting upto 10 values only...how to get that array existing element count as 10....

pls help me .....

i think you can use a Count variable and count the number of elements using a loop and print it. then also you can see..

i am using an string array with 100 length..but i am inserting upto 10 values only...how to get that array existing element count as 10....

I think the answer is on top of your post. strlen() should work.

if i allocate space like this
int no[20];
and i store about 10 nos in the array, how do i check the no of elements on the array ?

How do you store the 10 nos in the array?
something like

for ( i = 0 ; /*expression*/ ; i++ )
{
    no[i] = something ;
}

if so then i has the count.
or if its a hash table where numbers are not entered in order then I am afraid you will have to loop the array for valid entries.

Hope this helps:)

hope this will work.TC bye

int main()
{
    unsigned long arr[] = {123489,12789,12379,654561,544165,65654};
    int size = sizeof(arr)/sizeof(unsigned long);
    printf("%d",size);

}
commented: For bumping old threads. -1

This works, even if you later want to change the type of the array, say from char to int:

length_of_array = sizeof(array)/sizeof(array[0]);

It works because in C all of the elements of an array are of the same type.

The following program calculates the length of a character array and that of an integer array.

#include <stdio.h>

int main(void)
{
  char a[] = {'a','b','c'};
  int sa=sizeof(a);
  int la=sa/sizeof(a[0]);
  int b[] = {1,2,3};
  int sb=sizeof(b);
  int lb=sb/sizeof(b[0]);
  printf("sa %d la %d\n",sa, la);
  printf("sb %d lb %d\n",sb, lb);
  return 0;
}

The output is:
sa 3 la 3
sb 12 lb 3

(I know this reply is posted long after question was posted, but since I found the thread yesterday when I was looking for the answer, maybe somebody like me can use it.)

This works, even if you later want to change the type of the array, say from char to int:

length_of_array = sizeof(array)/sizeof(array[0]);

That doesn't work when the array is a parameter to a function

void foo(int ay[5])
{
int length_of_array = sizeof(ay)/sizeof(ay[0]);
printf("%d\n", length_of_array);
printf("sizeof(ay) = %d\n" ,sizeof(ay) );
printf("sizeof(ay[0]) = %d\n",sizeof(ay[0]));

}

One solution could be you could insert a '\0' /-1/some other invalid value after you are done inserting the numbers. Then in the function you can loop till you hit that invalid value

#include<stdio.h>
int main()
{
int a[]={18,29,3999,4444,56,6655,7667,89,9999,1056,1155,126};
int count=0;
int x=0;
while(a[count] != '\0')
{
++count;
}

for(x=0;x<=count-1;x++)
{printf("\n%d\n",a[x]);}

printf("\n---------------------------------------------\n");
printf("\nnumber of elements in array ....> %d\n",x);
printf("\n-----------------------.............---------\n");
}

@dewi44: Your program is 100 % wrong. If you want to post code, ok, but test it out before posting. If I were a teacher your program would get you a big fat 0.

#include<stdio.h>
int main()
{
int a[]={18,29,3999,4444,56,6655,7667,89,9999,1056,1155,126};
int count=0;
int x=0;
while(a[count] != '\0')
{
++count;
}

for(x=0;x<count-1;x++)
{printf("\n%d\n",a[x]);}

printf("\n---------------------------------------------\n");
printf("\nnumber of elements in array ....> %d\n",x);
printf("\n-----------------------.............---------\n");
}

why do you keep posting that dribble?

>>while(a[count] != '\0')
That is an infinite loop. Why? Because the array that is creted does not contain a value of '\0'. So the loop will just keep searching throughout the entire memory of your computer until it encounters the first byte that does contain the value 0.

the program works - end of story

I dont know which more worse. Your ignorance about C or your stubbornness

the program works - end of story

When I ran your program with vc++ 2010 express the value of count was 20. That clearly is not the correct size of the array. And here is the output

18
29
3999
4444
56
6655
7667
89
9999
1056
1155
126
-858993460
-688908683
3733924
596607
1
2298896
2300960

number of elements in array ....> 20
Press any key to continue . . .

thank you for that reply - i use GCC 4.3 compiler with Debian and the above code works perfectly with GCC 4.3 , BUT I CONCEDE that my code is far from perfect and i am not sure if using '/0' to find the end of an array is right. End of string yes, End of array i am not so sure.

do some compilers add extra elements in an array at the end as a kind of buffer?? - maybe if the array was converted to a pointer array , these extra elements would be removed.

Pure coincidence. I'm running OS X. With Clang2, it returns 14 elements. With GCC 4.2.1, it returns 13. No modification to the code. Also, if you're not sure, why are you so stubborn?

Unlike null-terminated character arrays there is no such thing as a terminating character in other kinds of arrays, such as int, float or double.

You can use the sizeof operator to calculate the number of elements in an int array tht is declared in the same function

int main()
{
   int a[] = {1,2,3,4,5};
   // the  number of elements in the array is
   // the size of the entire array divided by
   // the size of one of the elements.
   int sz = sizeof(a) / sizeof(a[0]);

}

The above will not work on pointers or arrays passed as a parameter to a function (such arrays are always treated as pointers).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.