435,082 Members | 2,135 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

Size of integer array

 P: n/a I have this code: void test(int* array) { int pp = sizeof(array); cout << pp << endl; } int main(){ int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }; int pp = sizeof(nums)/4; cout << pp << endl; test(nums); return 0; } In main I can take sizeof(nums)/4 which gives me 12 (correct number of elements in the array nums). But when I pass nums to test() and call sizeof again I only get 4. Why do I get 12 in main and 4 in test()? Apr 24 '07 #1
5 Replies

 P: n/a desktop wrote: I have this code: void test(int* array) { int pp = sizeof(array); cout << pp << endl; } int main(){ int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }; int pp = sizeof(nums)/4; cout << pp << endl; test(nums); return 0; } In main I can take sizeof(nums)/4 which gives me 12 (correct number of elements in the array nums). But when I pass nums to test() and call sizeof again I only get 4. Why do I get 12 in main and 4 in test()? Because the sizeof(int*), which is the function parameter, is 4 on your system. If you want the keep track of the size, you have to pass it, or just use std::vector. -- Ian Collins. Apr 24 '07 #2

 P: n/a On Tue, 24 Apr 2007 13:20:08 +0200, desktop wrote: I have this code: void test(int* array) { int pp = sizeof(array); cout << pp << endl; } int main(){ int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }; int pp = sizeof(nums)/4; cout << pp << endl; test(nums); return 0; } In main I can take sizeof(nums)/4 which gives me 12 (correct number of elements in the array nums). Yes. But when I pass nums to test() and call sizeof again I only get 4. Yes, because although you've called your parameter to test() "array", it's *not* an array - it's a pointer to int; and the size (in bytes) of a pointer to int on your system is evidently 4. Why do I get 12 in main and 4 in test()? Because pointers are not the same as arrays in C++. Note that when you pass in the array nums as an argument to test(), you are actually passing in a pointer to the first element of nums (this conversion happens implicitly); in fact the size of the array nums is simply not available to the function test(). You would have to pass it in as another parameter. -- Lionel B Apr 24 '07 #3

 P: n/a "Ian Collins" I have this code:void test(int* array){ int pp = sizeof(array); cout << pp << endl;}int main(){ int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }; int pp = sizeof(nums)/4; cout << pp << endl; test(nums); return 0;}In main I can take sizeof(nums)/4 which gives me 12 (correct number ofelements in the array nums).But when I pass nums to test() and call sizeof again I only get 4.Why do I get 12 in main and 4 in test()? Because the sizeof(int*), which is the function parameter, is 4 on your system. If you want the keep track of the size, you have to pass it, or just use std::vector. Or use a template function: template void test(int (&array)[N]) { cout << N << endl; } Of course, this limits the use of test() to only complete array types (no incomplete array types, e.g., extern int bla[], and no pointers) - Sylvester Apr 24 '07 #4

 P: n/a Sylvester Hesp wrote: > Or use a template function: template void test(int (&array)[N]) { cout << N << endl; } Of course, this limits the use of test() to only complete array types (no incomplete array types, e.g., extern int bla[], and no pointers) And it generates a different function for each array size that you call it on. -- -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference." (www.petebecker.com/tr1book) Apr 24 '07 #5

 P: n/a On Apr 24, 7:20 am, desktop #include void test(int* p_n) { std::cout << "type of p_n is "; std::cout << typeid(p_n).name(); std::cout << std::endl; } template< typename T, const size_t Size > void pass_by_ref(T (& array)[Size]) { std::cout << "type of array is "; std::cout << typeid(array).name(); std::cout << std::endl; } int main() { int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }; int sz = sizeof(nums)/sizeof(int); std::cout << "sz = " << sz; std::cout << std::endl; std::cout << "type of sz is "; std::cout << typeid(sz).name() << std::endl; std::cout << "type of nums is "; std::cout << typeid(nums).name(); std::cout << std::endl; test(nums); pass_by_ref(nums); return 0; } /* sz = 12 type of sz is i type of nums is A12_i type of p_n is Pi type of array is A12_i */ Apr 24 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion. 