0

I am trying to create an array of size 5, and take input to fill each index, then print out each input at each index. What I am getting is the array size to be multiples of 4. So when I input 5 as the size I get 20 instead of 5. What am I missing here?

#include <iostream>
using namespace std;

int main() {
    int userInput[5];
    cout << sizeof(userInput);

    // take input to fill array
    for (int i = 0; i < sizeof(userInput); i++) {
        cin >> userInput[i];
    }

    // print the array contents
    for (int i = 0; i < sizeof(userInput); i++) {
        cout << userInput[i]
    }
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 6
    Don't post pictures of your code. Post your actual code as text. – Hatted Rooster Aug 25 '18 at 20:56
  • An `int` is usually a 32 bit variable, hence takes 4 bytes. Therefore 5 ints take 20 bytes. You may want to have a look at https://en.cppreference.com/w/cpp/language/sizeof – Adrian W Aug 25 '18 at 21:00
  • @AdrianW You are assuming a byte is an octett. Mostly true nowadays, but mention that too. – Deduplicator Aug 25 '18 at 21:27
  • Note that arrays are fairly "low level". While they are important to know about, you probably want to build your awareness of `std::vector`, whose [.size()](https://en.cppreference.com/w/cpp/container/vector/size) gives what you would expect from a "higher-level" interface...and also lets you append and remove elements. – HostileFork says dont trust SE Aug 26 '18 at 04:31

3 Answers3

2

This is because sizeof:

Returns size in bytes of the object representation of type.

Not the size of the array. (int's are 4 bytes long)

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
2

From cppreference.com:

sizeof: Returns size in bytes of the object representation of type.

Now, since an int takes 4 bytes (this is implementation defined feature of compiler), thus an array of int[5] will have size 5*4=20 bytes.

If you want to get the size of an array, you can use (as pointed out by Deduplicator in comments):

std::size( userInput );

Don't forget to use #include <iterator> in this case.

Shubham
  • 2,847
  • 4
  • 24
  • 37
2

Alternatively, to get the size of a C array, use the std::size() function on C++17 capable compilers:

#include <iostream>

int main() {
    int userInput[5];
    int arrsize = static_cast<int>(std::size(userInput));

    for (auto i = 0; i < arrsize; i++) {
        std::cin >> userInput[i];
    }

    for (auto i = 0; i < arrsize; i++) {
        std::cout << userInput[i];
    }
}

You can simplify things with a range-based for loops:

#include <iostream>

int main() {
    int userInput[5];
    for (auto& el : userInput) {
        std::cin >> el;
    }
    for (auto el : userInput) {
        std::cout << el;
    }
}

Or replace the the C style array with a std::array wrapper:

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> userInput;
    for (auto& el : userInput) {
        std::cin >> el;
    }
    for (auto el : userInput) {
        std::cout << el;
    }
}
Ron
  • 14,674
  • 4
  • 34
  • 47