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
16 226070 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 -
T array[<SomeSize>];
-
-
size_t size_of_array = (sizeof array)/(sizeof array[0]);
-
Because C guarentees that sizeof char = 1 for char arrays this simplifies to -
char array[<SomeSize>];
-
-
size_t size_of_array = sizeof array;
-
I think you are confusing the size of a char array with the length of the string it contains -
char string[50] = "Hello World!";
-
-
if (strlen(string)+1 == sizeof string)
-
{
-
printf("String is exactly the right size for '%s'\n", string );
-
}
-
else if (strlen(string)+1 < sizeof string)
-
{
-
printf("String is bigger than required to hold '%s'\n", string );
-
}
-
else
-
{
-
printf("String is too small. DANGER Undefined Behaviour Envoked\n");
-
}
-
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
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.
-
-
int count(int thearray[]){
-
int count, i;
-
i=0;
-
-
count=0;
-
printf("%d\n", count);
-
while (thearray[i]!='\0'){
-
count++;
-
i++;
-
}
-
printf("%d\n", count);
-
return count;
-
}
-
-
int Max(int thearray[])
-
{
-
int max = thearray[0]; // first element be the max
-
//int length = sizeof(thearray);
-
-
int length = count(thearray);
-
int i;
-
-
printf("length is %d\n", length);
-
for(i = 1 ; i<length ;i++)
-
{
-
if (thearray[i] > max )
-
{
-
// if the elemnt at index is greater than
-
//previous max , than set this value to the max
-
max = thearray[i] ;
-
} //end of if
-
-
}//end of for
-
printf("Max is %d\n", max);
-
return max;
-
}
-
-
int main(){
-
//int thearray[100];
-
int max;
-
-
int thearray[] = {1,3,23,343,23,1212,23287,84547};
-
max=0;
-
printf("Max is %d\n", max);
-
max = Max(thearray);
-
printf("Max is %d\n", max);
-
system("PAUSE");
-
return 0;
-
}
-
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
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.
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
Thank you for all answers, I'll try
you can try this code
int arr[10];
int x;
x=sizeof(arr)
printf("%d",x/2);
:cool:
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
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).
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
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.
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.
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.
not necessary
this is compiler dependent
turboc assigns a size of 2 bytes to integer
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.
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
|
10 posts
views
Thread by Peter Stojkovic |
last post: by
|
1 post
views
Thread by Sivaraman.S |
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
| | | | | | | | | | | |