-
March 16th, 2007, 03:54 AM
#1
Get the size of an struct array?
Hello,
how to get the item count of an array of structs?
For example:
Code:
struct intArr
{
int i;
int y;
}
intArr ar[10]; // here!
sizeof(ar) is wrong!?
thanks
break;
-
March 16th, 2007, 03:58 AM
#2
Re: Get the size of an struct array?
Originally Posted by break;
Hello,
how to get the item count of an array of structs?
For example:
Code:
struct intArr
{
int i;
int y;
}
intArr ar[10]; // here!
sizeof(ar) is wrong!?
thanks
break;
Code:
sizeof(ar) / sizeof(ar[0]);
This only works if the code is placed where "ar" is really an array, not a pointer.
It will not work if you pass "ar"' to a function, and you attempt to get the number of elements from the passed in parameter. Passing an array to a function just passes a pointer, not the entire array.
Regards,
Paul McKenzie
-
March 16th, 2007, 05:18 AM
#3
Re: Get the size of an struct array?
Hello,
thanks for your help!
regards
break;
-
March 16th, 2007, 05:21 AM
#4
Re: Get the size of an struct array?
-
March 16th, 2007, 07:55 AM
#5
Re: Get the size of an struct array?
I like this method better:
Code:
sizeof(ar) / sizeof(intArr)
-
March 16th, 2007, 08:30 AM
#6
Re: Get the size of an struct array?
Or you could use the Standard Template Library
Code:
#include <vector>
struct intArr
{
int i;
int y;
}
std::vector<intArr> ar(10);
std::vector<intArr>::size_type size = ar.size();
Last edited by JohnW@Wessex; March 16th, 2007 at 09:49 AM.
-
March 16th, 2007, 12:02 PM
#7
Re: Get the size of an struct array?
Originally Posted by spoulson
I like this method better:
Code:
sizeof(ar) / sizeof(intArr)
Actually, I think that form is more confusing. Dividing the size of the entire array by the size of the first element is more intuitive.
My $0.02..
Viggy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|