Wednesday, 15 May 2013

C++ - the count of the array is irrelevant to the declared size -



C++ - the count of the array is irrelevant to the declared size -

i have written next code block. size of array returned sizeof() irrelevant. have array of 6 elements while sizeof() returns 24 size of array.

here code:

#include "stdafx.h" #include <iostream> using namespace std; int main() { int arr[6] = { 0, 5, 8, 32, 100, 41 }; int arrsize = sizeof(arr); int = 0; cout << "this size of array: " << arrsize << endl; /* (i = 0; < arrsize; ++i) { cout << arr[i] << endl; } */ homecoming 0; }

and here output:

this size of array: 24

you should do

int size = sizeof(arr)/sizeof(arr[0]);

this find total size of array ie. here 6 * 4 = 24 , split single element size, hence 24/4 = 6

c++ arrays

No comments:

Post a Comment