2

I'm trying to learn more about arrays since I'm new to programming. So I was playing with different parts of code and was trying to learn about three things, sizeof(). How do I find the length of an array, and also how do I put arrays into functions (as a parameter)?

My code is:

#include <iostream>

using namespace std;

void arrayprint(int inarray[])
{
    for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++)
    {
        cout << inarray[n_i] << endl;
    }
}

int main()
{
    int n_array[5];
    for (int n_i = 0 ; n_i < (sizeof(n_array) / sizeof(int)) ; n_i++ )
    {
        cin >> n_array[n_i];
    }
    arrayprint(n_array);
    return 0;
}

It seems the sizeof(inarray) / sizeof(int) works in the main function, but not in the arrayprint function. In the array print function, it evaluates to 1, while in main it evaluates to 5. Why?

So I guess what I want to know is how they are different? And why?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
samuraiseoul
  • 2,888
  • 9
  • 45
  • 65
  • possible duplicate of [Sizeof array passed as parameter](http://stackoverflow.com/questions/1328223/sizeof-array-passed-as-parameter) – David Rodríguez - dribeas Jul 11 '11 at 22:03
  • There are many duplicates of this question, you can search SO or in google for "array decay" in C++ – David Rodríguez - dribeas Jul 11 '11 at 22:04
  • 3
    You may find information mentioned here useful: http://stackoverflow.com/questions/1328223/sizeof-array-passed-as-parameter Basically, I would recommend to you to take a look at STL containers, especially vector, which is the preferred way to work with arrays in C++ programs – twoflower Jul 11 '11 at 22:05
  • [related FAQ](http://stackoverflow.com/questions/4810664/) – fredoverflow Jul 12 '11 at 05:35

3 Answers3

4

This

void arrayprint(int inarray[])

Is equivalent to this:

void arrayprint(int *inarray)

So you are getting the size of an int pointer on your machine. Even if your question is about C++ and your function looks a bit different, it boils down to this C FAQ entry.

So, inside main n_array is actually a true honest array. But when passed to a function, it "decays" into a pointer. And there's no way to know the real size. All you can do is pass additional arguments to your function.

void arrayprint(int inarray[], int len)
{
    for (int n_i = 0 ; n_i < len; n_i++)
    /* .... */

And call it like this:

arrayprint(n_array, sizeof(n_array) / sizeof(n_array[0]));

Of course, the real solution would be to use vectors since you're using C++. But I wouldn't know much about C++.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

What you've encountered was array-to-pointer conversion, a C language feature inherited by C++. There's a detailed C++-faq post about this: How do I use arrays in C++?

Since you're using C++, you could pass the array by reference:

template<size_t N>
void arrayprint(int (&inarray)[N])
{
    for (int n_i = 0 ; n_i < (sizeof(inarray) / sizeof(int)) ; n_i++)
        cout << inarray[n_i] << endl;
}

although in that case the sizeof calculation is redundant:

template<size_t N>
void arrayprint(int (&inarray)[N])
{
    for (size_t n_i = 0 ; n_i < N ; n_i++)
        cout << inarray[n_i] << '\n';
}

And, as already mentioned, if your goal is not specifically to pass a C array to a function, but to pass a container or a sequence of values, consider using std::vector and other containers and/or iterators.

Community
  • 1
  • 1
Cubbi
  • 46,567
  • 13
  • 103
  • 169
1

In main() your n_array is an array. When you take sizeof(n_array) you are getting the size of the array, in bytes. To be precise, you are getting the size such that sizeof(char) is identically equal to one. On most modern computers nowadays that is a byte.

An array is not a pointer. However, when you pass an array to some function what is passed is the address of the zeroth element of the array. The array degrades into a pointer. So your arrayprint() would be better prototyped as void arrayprint(int* inarray). When you take sizeof(inarray) you aren't computing the size of n_array in main(). You are computing the size of a pointer.

David Hammen
  • 32,454
  • 9
  • 60
  • 108