1
#include <stdio.h>
#include <iostream>

using namespace std;


int write(int arr[])
{

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

    for(int r=0;r<n;r++)
    {
        printf("%d\n", arr[r]);
    }

    return 0;
}

int main()
{
    int numlist[] = {1, 3, 5, 6, 7, 9, 0, 2, 3};

    write(numlist);

    std::string strvar;
    std::cin >> strvar;

}

What is being printed on the screen is just the number '1'. Nothing more. Just beginning C and I'm only trying to get the hang of the syntax.

Deanie
  • 2,316
  • 2
  • 19
  • 35
Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

6 Answers6

5

You cannot compute the size of the array inside the function using

(sizeof(arr)/sizeof(arr[0]));

That is because the function only sees arr as a pointer (the array decayed into a pointer), not as a full array. Thus, n will be 1.

You have to pass the size as an extra argument.

int write(int arr[], int n)
{
    for(int r=0;r<n;r++)
        printf("%d\n", arr[r]);

    return 0;
}

and call it with

write(numlist, sizeof(arr)/sizeof(arr[0]));
Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
3

Inside

int write(int arr[])

arr is a int*. sizeof(arr)/sizeof(arr[0]) comes out as 1 for you because pointers have the same size as integers on your system.

You can only calculate the number of elements in an array when you have access to its definition. In your case, this is inside main only. If you want to pass the array to another function you also need to pass its length

int write(int* arr, size_t num_elems)
{
    for(int r=0;r<num_elems;r++) {
        printf("%d\n", arr[r]);
    }
    return 0;
}

int main()
{
    int numlist[] = {1, 3, 5, 6, 7, 9, 0, 2, 3};
    size_t num_elems = sizeof(arr)/sizeof(arr[0]);
    write(numlist, num_elems);
}

Alternatively, if you define a sentinel value that marks the end of the array, you don't need to pass a length

int write(int* arr)
{
    while (*arr != -1) {
        printf("%d\n", *arr);
        arr++;
    }
    return 0;
}

int main()
{
    int numlist[] = {1, 3, 5, 6, 7, 9, 0, 2, 3, -1}; /* element with value -1
                                                        marks the end of the
                                                        array */
    write(numlist);
}
simonc
  • 41,632
  • 12
  • 85
  • 103
2

The problem is the following line

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

In this context C has no idea what the length of arr is. Arrays in C just don't have an inherent length property. Hence when you say sizeof(arr) it translates into 1 not the number of elements.

In order to make this work you need to explicitly pass the number of elements in the array to the function along with the array

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

int n = (sizeof(arr)/sizeof(arr[0])); does not work in this context, since arr is a function parameter which has decayed to a pointer at this point. Either pass the number of elements to the function or use a sentinel value to mark the end of the array.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

in C/C++, when arrayname passed as parameter, it's convert to an pointer. so in that function sizeof(arr) is and equal to sizeof(int*).

int C++

int a[10];
int (&b)[10] = a; // b is a reference of int array whose length is 10;

//so here could be ok
template< size_t n>
size_t write(int (& new_array_name)[n])
{
    //
}
surfree
  • 17
  • 2
1

On 64 bit Operating System, sizeof(arr[0]) is returning 4 bytes while sizeof(arr) is returning 2 bytes. On my system, n has value 2. Due to this reason, loop is executing r<2 times on my system. On 32 bit Operating system, sizeof(arr[0]) returns 2 bytes instead of 4 bytes. That's why you are getting index 0 of arr to be executed only.

Fahad Naeem
  • 515
  • 7
  • 15