Currently I have a several classes with an array defined as 'float myIDs'. I want to move the variable into my parent class and change it to a pointer ('float * myIDs').
Currently I'm declaring its values like this:
float myIDs[] = {
//Variables
};
As its now a pointer, I thought that it would be roughly the same:
myIDs = new float[] = {
};
but that doesnt seem to be working. I'm not sure how to solve this as I've never had to declare a pointer array like this before.
Can anyone help me please?
Thanks
6 Answers 6
Note that you're not allocating an array of pointer but just an array of float, so basically you two array would have the same type, they just won't be stored in the same memory space.
Only a statically allocated array can be initialized this way, a dynamically allocated one cannot be initialized to anything other than zero.
myIDs = new float[]();
But if you know the elements to put in the array, you don't need to allocate it dynamically.
If you want to allocate an array of pointer, you have to do this :
float* myIDs[size]; // statically
float** myIDs = new float*[size]; // dynamically
But only the statically allocated one (the first one) can be initialized the way you describe and of course, it must be initialized with pointers.
Comments
If you want to declare array in a dynamic way, you can do it like this:
float *array = new float[size];
array[0] = first_value;
array[1] = second_value;
etc;
Just remember to free memory when you no longer need it (e.g. in a class destructor)
delete [] array;
Comments
If you want a dynamically allocated array you should use the following format (what you did seems more like C# not C++)
//The declaration of the object in the class
float *myIDs;
//The allocation it self (you must know which size you want to allocate at this point)
myIDs = new float[size];//bring change "size" to whatever you need is.
Comments
Consider the following snippet,
#include<iostream>
#include<stdlib.h>
int main(void)
{
int a[] = {1,2};
a =new int[2];
delete(a);
return 0;
}
This gives an error error: incompatible types in assignment of ‘int*’ to ‘int [2]’.
I am statically creating an array of int . a is a pointer(it is of type int[2]) but it can't be used to point to other dynamically allocated arrays because they return pointer of type int*.
If you want to create an array dynamically you have to assign its address to a float*
float * a = new float[10] ;
Refer this too.
Comments
The easiest way is:
float *myArray[size];
Example
#include <iostream>
using namespace std;
float* findMax (float* myArray[], int size) {
float max = 0;
int index = 0;
for (int i = 0; i < size; i++) {
if ( *myArray[i] > max) {
max = *myArray[i];
index = i;
}
}
return myArray[index];
}
int main()
{
float a = 1.25;
float b = 2.47;
float c = 3.92;
float d = 4.67;
float e = 5.89;
float f = 6.01;
float *myArray[6];
int len = *(&myArray + 1) - myArray;
myArray[0] = &a;
myArray[1] = &b;
myArray[2] = &c;
myArray[3] = &d;
myArray[4] = &e;
myArray[5] = &f;
cout << "Number of even values are : " << findMax(myArray, len) << endl;
return 0;
}
Comments
If you want an array of pointers to float, you must declare it as such. You declared just an array of floats. The name of the array is a pointer of course, but in the C sytnax it is treated the same and just a convenience.
float *myIDs[] = {
//Variables
};
myIDs = new *float[n] = {
};
Alternatively you can use
float **myIDs;
myIDs = new **float;
And access it the same way like an array:
float *x = myIDs[i];
3 Comments
new *float[n] and new **float do the same thing, not even for n==1. Besides, I doubt the C++ language has something like new *float[n] = {}.new float*[n] and cannot be initialized to anything other than zero. In your second example, new float** would return a float*** and allocate only the memory of one float**.
std::vector<float>if you want a dynamic array?