2

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

3
  • "As its now a pointer" - no it's not, it's an array. And why not use std::vector<float> if you want a dynamic array? Commented Apr 17, 2013 at 7:12
  • 1
    There is a lot of confusion with this questions as you have asked for 'pointer array', and it's hard to tell if you are asking for a pointer to an array (which can be deducted from the text), or array of pointers (from the title). BTW. by writing `myIDs = new float[] = {}; you are trying to assign a new value to whatever new float[] returns. Commented Apr 17, 2013 at 7:44
  • Sorry for the confusion I meant that its a pointer to an array as opposed to an array of pointers. Commented Apr 17, 2013 at 7:57

6 Answers 6

8

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

0

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

0

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

0

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

-4

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

I don't think 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] = {}.
What you wrote is wrong, in your first example it should be 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**.
OK, I was a bit hasty. The concept stil works though. Allocating a pointer array with "**" and using it as *[i] is valid though. Of courye you have to manage the memory yourself and initialize the pointers in your code. Automatic initialization doesn't work of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.