468,490 Members | 2,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

how to know size of integer array?

Been bothering me so long, we can find the size of string array (array of char) but what is the way to find the size for integer array? I've tried using while loop until it find '\0' null character, it's stop whenever integer 0 is encountered, which is not what I want .. anyone please help me
Aug 3 '06 #1
16 225860
Banfa
9,055 Expert Mod 8TB
You find the size of an integer array in the same way that you find the size of a char array or the size of any array, using the sizeof operator

So for any type T

Expand|Select|Wrap|Line Numbers
  1. T array[<SomeSize>];
  2.  
  3. size_t size_of_array = (sizeof array)/(sizeof array[0]);
  4.  
Because C guarentees that sizeof char = 1 for char arrays this simplifies to

Expand|Select|Wrap|Line Numbers
  1. char array[<SomeSize>];
  2.  
  3. size_t size_of_array = sizeof array;
  4.  
I think you are confusing the size of a char array with the length of the string it contains

Expand|Select|Wrap|Line Numbers
  1. char string[50] = "Hello World!";
  2.  
  3. if (strlen(string)+1 == sizeof string)
  4. {
  5.     printf("String is exactly the right size for '%s'\n", string );
  6. }
  7. else if (strlen(string)+1 < sizeof string)
  8. {
  9.     printf("String is bigger than required to hold '%s'\n", string );
  10. }
  11. else
  12. {
  13.     printf("String is too small. DANGER Undefined Behaviour Envoked\n");
  14. }
  15.  
Aug 3 '06 #2
thank you for your answer, but what I need to find is the size of array (length of array of integer) maybe I said it not clear enough, sorry

I've used sizeof [n] but it returns me same value each time (4) I don't know why
where n is array of integer, which I assign numbers of integer into it

please help
Aug 3 '06 #3
Banfa
9,055 Expert Mod 8TB
I have given you the correct solution in my previous post. what you have just posted does not make sense without you specifying what [n] is.

If what you say is true for

int array[5];

sizeof(array) == 4

then there is a bug in your compiler.

I suggest however that you post the actual code you are using as it is more likely that there is a different error, for instance is it really and array or is it a pointer to an int to which you are assigning memory for an array of integers.
Aug 3 '06 #4
Expand|Select|Wrap|Line Numbers
  1.  
  2. int count(int thearray[]){
  3.     int count, i;
  4.     i=0;
  5.  
  6.     count=0;
  7.     printf("%d\n", count);
  8.     while (thearray[i]!='\0'){
  9.           count++;
  10.           i++;
  11.     }
  12.     printf("%d\n", count);
  13.     return count;
  14. }
  15.  
  16. int Max(int thearray[])
  17. {
  18.     int max = thearray[0];    // first element be the max
  19. //int length = sizeof(thearray);
  20.  
  21.     int length = count(thearray);
  22.     int i;
  23.  
  24.     printf("length is %d\n", length);
  25.     for(i = 1 ; i<length ;i++)
  26.     {
  27.         if (thearray[i] >  max )
  28.         {
  29.             // if the elemnt at index   is greater than
  30.             //previous max , than set this value to the max
  31.             max = thearray[i] ;
  32.         } //end of if 
  33.  
  34.     }//end of for 
  35. printf("Max is %d\n", max);
  36.     return max;
  37. }
  38.  
  39. int main(){
  40. //int thearray[100];
  41. int max;
  42.  
  43. int thearray[] = {1,3,23,343,23,1212,23287,84547};
  44. max=0;
  45. printf("Max is %d\n", max);
  46. max = Max(thearray);
  47. printf("Max is %d\n", max);
  48. system("PAUSE");    
  49.     return 0;
  50. }
  51.  
My code so far it doesn't work right both way (using count function or sizeof array)

Hope there is no bug in my compiler though
Aug 3 '06 #5
Banfa
9,055 Expert Mod 8TB
In either of the prototypes

int count(int thearray[])
int Max(int thearray[])

thearray is declared as a pointer, you are probably getting confused because you are using the [] notation but this does not declare an array this is just another way of writting

int Max(int *thearray)

and a fairly confusing one as you have found out. sizeof(int *) == 4 on a lot of systems (and so it seems on yours).

You can not find the size of an array from a pointer to it, it is impossible, you have to have access to the array it self to find it's size. This means that if you are goinf to pass an array by passing a pointer to it (a normal thing to do) then you need to pass the size of the array as well. The one acception to this is char array where it is being use to store a string because you can use the NULL terminator '\0' to locate the end of the string (but not the size of the array).

What you should do is change your prototype of Max to

int Max(int *thearray, int number_of_ints)

and in main (where thearray is defined) call Max as

max = Max(thearray, (sizeof thearray)/(sizeof thearry[0]));

This will work because you are using the sizeof opertor in the same context that the array is declared in.
Aug 3 '06 #6
Been bothering me so long, we can find the size of string array (array of char) but what is the way to find the size for integer array? I've tried using while loop until it find '\0' null character, it's stop whenever integer 0 is encountered, which is not what I want .. anyone please help me


u can use sizeof operator to know about the sizeof array in terms of no of bytes then divide it by size of integer ,u will get the no of elements in the array
Aug 4 '06 #7
Thank you for all answers, I'll try
Aug 4 '06 #8
you can try this code

int arr[10];
int x;
x=sizeof(arr)
printf("%d",x/2);

:cool:
Aug 7 '06 #9
Banfa
9,055 Expert Mod 8TB
you can try this code

int arr[10];
int x;
x=sizeof(arr)
printf("%d",x/2);
This code makes an assumtion about the size of an integer and it wont give the right answer if sizeof int != 2
Aug 7 '06 #10
thank you for your answer, but what I need to find is the size of array (length of array of integer) maybe I said it not clear enough, sorry

I've used sizeof [n] but it returns me same value each time (4) I don't know why
where n is array of integer, which I assign numbers of integer into it

please help
If n is an integer,
sizeof(n) will always give 4,
but if n is an array of integers,
sizeof(n) will give (4*number of elements in n).
Aug 7 '06 #11
Hi

Getting integer array size is prettry simple
Lets say I have int array i[5]

then
sizeof(i)/sizeof(i[0]) will give me 5 i.e. the length of integer array
Aug 11 '06 #12
I see a miscommunication. If the array is declared with an explicit size, as in a[5], the compiler knows it is an array and knows how big it is, so sizeof() returns the total size of the array. If the array is incompletely declared, as in a[] (commonly used for a parameter or a const-initialized array), then the compiler only knows that "a" is a POINTER, not the array itself, and the sizeof refers to the POINTER.
Oct 4 '10 #13
Bugdog
1
Are you sure? I do understand that it may depend on standards and how they are implemented by compilers, but when I declare an array like this:

const int arr[] = { 1,2,3,4,5,6,7,8,9,10 };

The sizeof(arr) gives me 4 * 10, provided that sizeof(int) in my system is 4.
Aug 24 '11 #14
Banfa
9,055 Expert Mod 8TB
@Bugdog you are correct, when you declare an array without specifying a size, i.e. when you actually are creating an array then the sizeof operator will give to correct size because the name actually is the name of an array.

when you using the [] notation in a function parameter list it is exactly the same as declaring a pointer (which can be treated very similarly to arrays). The name is a name of a pointer and not the name of an array and you get the size of the pointer if you use sizeof.
Sep 6 '11 #15
mukul
1
not necessary

this is compiler dependent

turboc assigns a size of 2 bytes to integer
Nov 8 '11 #16
lspxy
12
because the formal parameter when functions are called is just a copy of actual parameter, so sizof() is just a size of a pointer.
if the name of an array is operand of sizeof, then it returns the size of total array. so, it has some different.
Jan 2 '12 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by cxcodexc | last post: by
11 posts views Thread by Sontu | last post: by
12 posts views Thread by manochavishal | last post: by
7 posts views Thread by rhitz1218 | last post: by
5 posts views Thread by desktop | last post: by
16 posts views Thread by danieleghisi | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.