### Thread: finding size of integer array?

Page 1 of 2 12 Last
1. #### finding size of integer array?

strlen doesn't work for finding the size of a integer array does it? I have something like this:

int x[] = {1,4,33,4};
int y = strlen(x);

cout << y;

I am expecting Y to say "4" but my compiler just gives me a weird error. Ideas? Im on windows2000, MSVC..Thanks.

-andy
2. if an integer is 4 bytes, then an array of 4 integers would be 16 bytes long. here from this example we can see:
Code:
```#include<stdio.h>

int main(int argc, char **argv)
{
int array[4] = {1,2,3,4};

printf("%p %p %p %p\n", &array[0], &array[1], &array[2], &array[3]);

unsigned int diff = (unsigned int)&array[3] - (unsigned int)&array[0];

printf("diff is %x\n", diff);

return 1;
}```
0xbffffab0 0xbffffab4 0xbffffab8 0xbffffabc
diff is c

-so between the first and last element there is 12 bytes. + an extra 4 for teh last element and we have 16:)
Last edited by infamous41md; June 15th, 2003 at 12:56 AM.
3. hmm..maybe i didn't explain myself right. I don't care how many bytes are in the array..i just wanna know if there is a way I can find out how many elements an integer array has in it. My first example explains that pretty well. Thanks for your input though i actually learned something new through it.

-andy
4. Use the sizeof operator instead:
int x[] = {1,4,33,4};
int y = sizeof (x) / sizeof (x[0]);

Note that this will not work if your arrays is allocated with malloc, calloc, new, strdup or anything like that.
5. No Profile Picture
status unknown
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
262
Rep Power
15
Of course if you're using C++, as the OP was, then you can use a vector instead of an array, Then vector.size() gives you the number of elements. It's worth considering using vector instead of array as a general case.
6. correct, I can also use a matrix. But for some reason it seems that vector's and matrix's arent used often. Why is this?
7. No Profile Picture
status unknown
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
262
Rep Power
15
I'm not sure why you think vectors aren't used often, but in my experience they are. I think for historical reasons many courses teach arrays before vectors, or teach arrays only, but for general use I would suggest using vectors (or generalising, you should prefer to use the containers in the C++ standard library). You should still know how to use both, of course.
8. #### Re: finding size of integer array?

Originally posted by andy3109
strlen doesn't work for finding the size of a integer array does it? I have something like this:

int x[] = {1,4,33,4};
int y = strlen(x);

cout << y;

I am expecting Y to say "4" but my compiler just gives me a weird error. Ideas? Im on windows2000, MSVC..Thanks.

-andy
No, strlen does not work. While everybody else have given you good info on solving your problem, they did not address your basic mistake.

The thing is that strlen works on character arrays and depends on basic assumptions about C-style strings (AKA "char arrays"), especially that they be null-terminated. Because of those basic assumptions, trying to use strlen on non-char arrays will yield unexpected results, which is what you got.

Bits are bits and bytes are bytes. There is no intrinsic difference between characters, integers, floating-point numbers, pointers, or machine instruction op codes; if you see a byte taken out of context, you will have no idea what data type it is a part of. We tell the computer through our program how it will interpret those bytes.

So if you tell the computer that that integer array is an array of characters, then it will interpret each byte on each integer as a character (in reverse byte order on an Intel). strlen will count each "character" until it finds the null-terminator, a byte containing the value 0x00.

But that would only have been if you could have gotten your program to run. You say you got a weird error. Did it have anything to do with trying to feed strlen an int pointer when it expects a char pointer?
9. that actually was the error. But it was related to using strlen with an int pointer.
10. ok, im running into problems. I designing a sort of a sort algorithm that is very similar to bubble sort...The output, nothing just a blank dos screen that has a blinker on it. It is related to the "sizeof" line im possitive.. Here is the code:

int i=0,x,temp;
float thearray[] = {90,54,90,33,44,56, '\n'};
float amt;
amt = sizeof(thearray);
for (x = 0; x<amt; x++)
{
for (i=0; i<amt; i++)
{
if (thearray[i] > thearray[i+1])
{

temp = thearray[i+1];
thearray[i+1] = thearray[i];
thearray[i] = temp;
}
}
}
for (i=0; i<amt; i++)
{
cout << thearray[i] << "\n";
}
system("pause");

}
11. You're computing amt incorrectly.
amt = sizeof (thearray) / sizeof(thearray[0]);

BTW, shouldn't amt be of type int too. Also how did you manage to get the compiler to not complain about '\n' in the declaration of thearray :)
12. idk..it doesn't complain though :)

Why "/ sizeof(thearray[0]);"? What does that do?
Last edited by andy3109; June 15th, 2003 at 10:21 PM.
13. Code:
```float array[5] = {1.0,2.1,3.3,4.4,5.5};
int size = sizeof(array)/sizeof(float);

printf("size is %d\n", size);```
-when you do sizeof(array) it returns to you the total size in bytes of your array. what you said you wanted is the number of elements in the array. so you must divide the total size by the size of each element. this is what scorpions was showing you. you could also do it the way i put above.
14. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Posts
272
Rep Power
22
Also how did you manage to get the compiler to not complain about '\n' in the declaration of thearray
'\n' is just another byte value. The compiler shouldn't complain about it. What I am curious about is what purpose does it serve in an array of floats?
15. well originaly I used i+1 for my algorithm which was flawed because on the last test to organize the #ers within the array, there was nothing to compare. So I compared it with a null character (which didn't effect order). But I ended up just switching to i-1 and got rid of the ending '\n'.
Page 1 of 2 12 Last