a value of type "int *" cannot be assigned to an entity of type "int"

Trying to do the following task: Write a function that accepts an int array and the array size as arguments. The function should dynamically create a new array that is the same size as the array passed in. Each element in the new array should be double the corresponding element in the array passed in. Return a pointer to the new array.
E.g. if you pass in the array {2, -3, 5} of size 3, it should create a new array of size 3 with values {4, -6, 10} and return a pointer to it.


But got this error "a value of type "int *" cannot be assigned to an entity of type "int" on line 19.
Can someone explain it to me and how to fix it?

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
#include <iostream> // 

#include <string>
using namespace std;

int *doubler(int *arr[], int size)
{
    //dynamically allocate an array
    int *arrPtr = new int[size];

    cout << "SIZE ?\n";
    cin >> size;

    int x = 0;
    for (int i = 0; i < size; i++)
    {
        cout << "Enter your value :\n";
        cin >> x;
        *(arrPtr + x) = *(arr + x);
    }
    delete[] arrPtr;
    return arrPtr;
}

int main()
{

    doubler();
    return 0;
}
Last edited on
Declare the parameter arr as either
 
int *doubler(int *arr, int size)
or
 
int *doubler(int arr[], int size)
They mean the same thing: a pointer to int.

This:
 
int *doubler(int *arr[], int size)
is the same as this:
 
int *doubler(int **arr, int size)
I.e. A pointer to a pointer to int.

By the way, you main() function is calling the doubler() function incorrectly.
@OP

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
#include <iostream>

using namespace std;

int* doubler(int* some_array, size_t size)
{
    int* doubled_array = new int[size];
    for (int i = 0; i < size; i++)
    {
        doubled_array[i] = some_array[i] * 2;
    }

    return doubled_array;
}

int main()
{
    int oldArray[]{2,-3,5};
    size_t old_size = sizeof(oldArray)/sizeof(int);

    int* newArray = doubler(oldArray, old_size);

    for (int i = 0; i < old_size; i++)
    {
        cout << newArray[i] << ' ';
    }
    cout << '\n';

    delete[] newArray; // <--

    return 0;
}



4 -6 10 
Program ended with exit code: 0

See: https://stackoverflow.com/questions/12992925/c-correct-way-to-return-pointer-to-array-from-function
@siid14. L21 you are deleting the allocated memory and then returning it as a pointer! No. You are returning a pointer to now unallocated memory. The delete should be in the calling function after the memory has been used.

It is also easier to use std::unique_ptr as this doesn't require explicit deletion:

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

auto doubler(const int arr[], size_t arrsz)
{
	auto doubled {std::make_unique<int[]>(arrsz)};

	for (size_t i = 0; i < arrsz; ++i)
		doubled[i] = arr[i] * 2;

	return doubled;
}

int main()
{
	const int oldArray[] {2, -3, 5};
	const auto newArray {doubler(oldArray, std::size(oldArray))};

	for (size_t i = 0; i < std::size(oldArray); ++i)
		std::cout << newArray[i] << ' ';

	std::cout << '\n';
}

Thank you guys, it's really helpful
Topic archived. No new replies allowed.