To create a variable that will point to a dynamically allocated array, declare it as a pointer to the element type. For example, int* a = NULL; // pointer to an int, intiallly to nothing. A dynamically allocated array is declared as a pointer, and must not use the fixed array size declaration.
Dynamically allocating an array of pointers follows the same rule as arrays of any type: type *p; p = malloc(m* sizeof *p); In this case type is float * so the code is: float **p; p = malloc(m * sizeof *p);
How to create a dynamic array of pointers (to integers) of size 10 using new in C++? Hint: We can create a non-dynamic array using int *arr[10] A. int *arr = new int *[10];
There's no built-in dynamic array in C, you'll just have to write one yourself. In C++, you can use the built-in std::vector class.
I have the following class
class Node
{
int key;
Node**Nptr;
public:
Node(int maxsize,int k);
};
Node::Node(int maxsize,int k)
{
//here i want to dynamically allocate the array of pointers of maxsize
key=k;
}
Please tell me how I can dynamically allocate an array of pointers in the constructor -- the size of this array would be maxsize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With