2

I need to create an array in function and i can't declare its size in main, I'm starting my adventure with C++ I wrote this:

#include <iostream>

using namespace std;

int create(int * n);
void display(int * arr, int * n);

int main()
{
    int * n = new int;
    int * arr = create(n);
    display(arr, n);

    delete n;
    delete[] arr;

    return 0;
}

int create(int * n) {
    cout << "How many elements? ";
    cin >> *n;
    int * arr = new int[*n];
    int element;
    for(int i = 0; i < *n; i++) {
        cout << endl << "Enter " << i << " array element: ";
        cin >> element;
        arr[i] = element;
    }

    return arr;
}

void display(int * arr, int * n) {
    for(int i = 0; i < *n; i++) {
        cout << endl << i << " element: " << arr[i];
    }
}

Compiler error: error: invalid conversion from 'int*' to 'int' [-fpermissive] Help me please understand what am I doing wrong here. Thank you! :)

Sz3jdii
  • 507
  • 8
  • 23
  • Use `std::vector`, from the `` header. Return it from the function. – Cheers and hth. - Alf Mar 25 '18 at 15:49
  • Use a `std::vector` already rather than a C-style array. – Jesper Juhl Mar 25 '18 at 15:49
  • @user202729 I really don't get where i do sth wrong (where this wrong conversion of int* to int is located) – Sz3jdii Mar 25 '18 at 15:50
  • 1
    Seriously -- if you're writing C++ code this way, something is wrong. Which tutorial are you using? – user202729 Mar 25 '18 at 15:51
  • 2
    You assign your array to an `int*` but your function says it returns an `int`. The function should say it returns an `int*`. – Galik Mar 25 '18 at 15:53
  • @user202729 This https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb – Sz3jdii Mar 25 '18 at 15:54
  • And your function doesn't need to receive a parameter. – Galik Mar 25 '18 at 15:54
  • @Galik Oh now i mostly get it :) Sorry guys but pointer things are difficult for me.. – Sz3jdii Mar 25 '18 at 15:55
  • 1
    I asked just out of curiosity, anyway. However your code feels terribly wrong, consider posting to [codereview.se] to receive advice how to make your code better. Remember to read their help center and How to ask page before asking, especially the "posted code must be working" part. – user202729 Mar 25 '18 at 15:57
  • Advice: If you don't need to use pointer, don't use them. – user202729 Mar 25 '18 at 15:57
  • @user202729 Thaank you for your advice i will do that :) – Sz3jdii Mar 25 '18 at 15:58
  • @Beta can you tell me why? I will post my code later to code review anyway, because i don't want to learn programming bad and learn bad habbits... – Sz3jdii Mar 25 '18 at 16:01
  • [Code review: What topics can I ask about here?](https://codereview.stackexchange.com/help/on-topic) – user202729 Mar 25 '18 at 16:04
  • **I eat my words.** At first glance, I thought you were making a serious error, but in fact I was wrong, you're just using pointers in a needlessly complicated way. I suggest that for the second parameter you pass an `int` rather than `int*`. Also, you can look into reference arguments (e.g. `int create(int &n)`). – Beta Mar 25 '18 at 16:08
  • @Beta Ok :) Thank you for your time spending here with my problem i''l try to change my program to be better. – Sz3jdii Mar 25 '18 at 16:10
  • Recommend learning from a [good book](https://stackoverflow.com/q/388242/9254539) instead of online tutorials which are generally terrible so you don't end up with problems like these in the future. – eesiraed Mar 25 '18 at 16:14
  • @FeiXiang I'll keep that in mind and check all of them ;) – Sz3jdii Mar 25 '18 at 16:15

1 Answers1

3

You try to return an int from the create() function, but you return an array. A simple fix is to change the signature of create() to:

int* create(int* n);
MivVG
  • 679
  • 4
  • 16