473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 226556
Banfa
9,065 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,065 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,065 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,065 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,065 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

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

Similar topics

6
by: cxcodexc | last post by:
I am trying to overload the +, *, and - operators using variables of type int but allowing them to hold unlimited size. Can anyone help me do this? thanks
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
10
by: Peter Stojkovic | last post by:
I want store an integer-array of 1000 Values in a blob in a SQL-database. I will do this every 10 Seconds. How can I do this ???? What datatypes a have to use ??? Thanks
1
by: Sivaraman.S | last post by:
Hi, Can i pass integer array to methodInfo.Invoke(obj,args()). I am able to pass only string array to this function. This is the code i have written. Dim myType As Type = objClass.GetType()...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
7
by: rhitz1218 | last post by:
Hi all: Is it possible to store an integer array to another integer array? Or should I use an array of pointers that will point to a particular array? Thanks for the help.
5
by: desktop | last post by:
I have a function that takes two pointers to an array. The first point to the first element while the other points to the last element. int nums = { 1, 2, 3, 4, 5, 7, 8, 9}; int* result; int...
16
by: danieleghisi | last post by:
Hello I'm writing a small programme to look for some particular integer sets (homometric sets), and each one of this sets is a unsigned long long (e.g. {1, 2, 4, 7} --1001011=75). I need to have...
18
by: nk28 | last post by:
my question is that i have 512 mb ram .What is the max integer array size that i can declare in c. I recently experimented with different values and found that while i am able to create large values...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.