0

I'm learning pointers in but I'm stuck on dynamic allocation of arrays.

The code below provides a function to find the element with the lowest value. A dynamically allocated array is passed as a parameter to it.

#include <cstdlib>
#include <iostream>

using namespace std;

int findMin(int *arr, int n);

int main()
{
    int *nums = new int[5];
    int nums_size = sizeof(*nums);

    cout << "Enter 5 numbers to find the minor:" << endl;
    for(int i = 0; i < nums_size; i++)
        cin >> nums[i];

    cout << "The minor number is " << findMin(*nums, nums_size);

    delete [] nums; 

    return 0;
}

But it return this error:

error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]

How can I pass that array to the function?

Just for curiosity: why the for loop allows me to enter 4 value if my array is made up of 5 elements?

Jabberwocky
  • 40,411
  • 16
  • 50
  • 92
UnoaCaso
  • 47
  • 6
  • Notice that the `new` is not needed in this case. You should allocate it on the stack using `int[] nums[5];` instead - it is faster, and doesn't allocate memory on the heap that you need to explicitly delete. – Kerek May 26 '20 at 13:41
  • `*nums` is an `int`. The `*` is part of the type, not of the variable identifier. – molbdnilo May 26 '20 at 14:05
  • Think if you had written `int* nums = createArray()`. There's no way to know at compile-time from just a pointer to an integer how many integers it points to. And maybe you didn't realize that [`sizeof` is a compile-time thing](https://softwareengineering.stackexchange.com/questions/195386/why-is-sizeof-called-a-compile-time-operator), not a run-time thing. – Wyck May 26 '20 at 14:57

2 Answers2

9

How can I pass that array to the function?

nums is already a type int*, you don't need to dereference it:

findMin(nums, nums_size);

why the for loop allows me to enter 4 value if my array is made up of 5 elements?

int nums_size = sizeof(*nums); does not do what you think it does. It's equivalent to sizeof(nums[0]), which is equivalent to sizeof(int), which happens to be equal to 4 at your machine.
There is no way to extract size of array allocated on the heap, you need to save the size on your own:

int nums_size = 5;
int* nums = new int[nums_size];
Yksisarvinen
  • 13,037
  • 1
  • 18
  • 42
0
#include <cstdlib>
#include <iostream>

using namespace std;

int findMin(int *arr, int n){
    int mn=INT_MAX;
    for(int i=0;i<n;i++){
        if(arr[i]<mn){
            mn=arr[i];
        }
    }
    return mn;
};

int main()
{
    int nums_size = 5; 
    int *nums = new int[nums_size];

    cout << "Enter 5 numbers to find the minor:" << endl;
    for(int i = 0; i < nums_size; i++)
        cin >> nums[i];

    cout << "The minor number is " << findMin(nums, nums_size);

    delete [] nums; 

    return 0;
}

The above code works fine. Your error was in passing the array to the function.

Also to add -

Your code made only 4 iterations coz sizeof(*nums) returned the size of base index element pointed by pointer, i.e ,sizeof(num[0]). So I made a minor change and now it works fine.

Abhishek Bhagate
  • 5,033
  • 3
  • 10
  • 29