lundi 4 mars 2019

Why functions dealing with pointers requires * (asterix) in front of function name in C++?

The following code gives me an error when I remove asterisk * from function name, it this a conventional rule?

This function allocates memory on the heap and returns a pointer pointing to the initial location of the heap.

int *create_array (size_t size, int int_value = 0)
{
    int *new_storage {nullptr};
    new_storage = new int [size];
    for (size_t i{0}; i < size; ++i)
        *(new_storage + i) = int_value;
    return new_storage;

}

Aucun commentaire:

Enregistrer un commentaire