Reputation: 299
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
Upvotes: 2
Views: 49134
Reputation: 454
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;
}
Upvotes: 0
Reputation: 415
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;
Upvotes: 5
Reputation: 10698
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.
Upvotes: 8
Reputation: 5239
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.
Upvotes: 0
Reputation: 22094
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];
Upvotes: -4
Reputation: 19463
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.
Upvotes: 0