A question on sizeof

Trying to practice recursive function.
Not sure why the expression sizeof(p) / sizeof(p[0]) cannot be used here.

When compiling, it gives out "CH15.5.cpp: In function 'void sumArray(int*, int&, int&)':
CH15.5.cpp:21:27: warning: 'sizeof' on array function parameter 'p' will return size of 'int*' [-Wsizeof-array-argument]
21 | if(counter < (sizeof(p) / sizeof(p[0])))
| ^
CH15.5.cpp:19:19: note: declared here
19 | void sumArray(int p[],int & counter,int& sum)
| ~~~~^~~
"

Thanks in advance.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

void sumArray(int p[],int & counter,int &sum);

int main()
{
    int  p[10]; 
    int counter = 0, sum = 0; 

    for(int k = 0; k < 10; k++)
    {
        p[k] = k*k;
    }

    sumArray(p,counter,sum);
    std::cout << sum; 
}

void sumArray(int p[],int & counter,int& sum)
{
    if(counter < (sizeof(p) / sizeof(p[0])))
    {
        sum += p[counter];
        counter++; 

        sumArray(p,counter,sum);
    }
        
}
Last edited on
The size of p is unknown at compile time as the size isn't specified as part of the parameter. sizeof requires that the size is known at compile time.

For this to compile, you need to specify int p[10] in the function declaration/definition.
Last edited on
@seeplus
Thanks.
Not sure I'm getting this. But after changing all p[] to p[10], I still have the same output.
1
2
3
4
5
6
CH15.5.cpp: In function 'void sumArray(int*, int&, int&)':
CH15.5.cpp:21:27: warning: 'sizeof' on array function parameter 'p' will return size of 'int*' [-Wsizeof-array-argument]
   21 |     if(counter < (sizeof(p) / sizeof(p[0])))
      |                           ^
CH15.5.cpp:19:19: note: declared here
   19 | void sumArray(int p[10],int& counter,int& sum)
Sorry, p is passed as a pointer in this case. So sizeof(p) is returning the size of a pointer - not the size of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

void sumArray(int p[10], int& counter, int& sum);

int main()
{
	int  p[10] {};
	int counter {}, sum {};

	for (int k = 0; k < 10; ++k)
		p[k] = k * k;

	sumArray(p, counter, sum);
	std::cout << sum;
}

void sumArray(int p[10], int& counter, int& sum)
{
	if (counter < 10) {
		sum += p[counter];
		++counter;

		sumArray(p, counter, sum);
	}
}

To pass an array of an unknown size as a function parameter and to determine the passed size, use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

template<size_t N>
void sumArray(const int (&p)[N], int& counter, int& sum);

int main()
{
	int  p[10] {};
	int counter {}, sum {};

	for (int k = 0; k < 10; ++k)
		p[k] = k * k;

	sumArray(p, counter, sum);
	std::cout << sum;
}

template<size_t N>
void sumArray(const int (&p)[N], int& counter, int& sum)
{
	if (counter < N) {
		sum += p[counter];
		++counter;

		sumArray(p, counter, sum);
	}
}

Hello y19177,

Working off your code try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

constexpr int MAXSIZE{ 10 };

using NUMARR = int[MAXSIZE];

void sumArray(const NUMARR& numArr, int & counter, int &sum);

int main()
{
    NUMARR numArr{};
    int counter = 0, sum = 0;

    for (int k = 0; k < 10; k++)
    {
        numArr[k] = k * k;
    }

    sumArray(numArr, counter, sum);

    std::cout << sum;

    return 0;
}

void sumArray(const NUMARR& numArr, int& counter, int& sum)
{
    if (counter < (sizeof(numArr) / sizeof(numArr[0])))
    {
        sum += numArr[counter];
        counter++;

        sumArray(numArr, counter, sum);
    }

}


And please come up with better names other than "p".

Andy
Thanks everyone. Seems like best solution is to use a number.
the best solution is to use something that knows how big it is, like a <vector>.
arrays have a place of course, but std::array also knows its own size, so the raw C style arrays are less useful for most tasks. I still use them, but not if I have to pass the size around or compute it ... more for small lookup tables that have a fixed size.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int sumArray( int *p, int n );

int main()
{
   int p[] = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
   std::cout << sumArray( p, sizeof p / sizeof p[0] );
}

int sumArray( int *p, int n )
{
   if ( !n ) return 0;
   return *p + sumArray( p + 1, n - 1);
}
Topic archived. No new replies allowed.