pointer to array not recognised as array? 
Author Message
 pointer to array not recognised as array?

Hi all,

to be able to use a function of an existing library, i have to pass it an
array of unsigned longs. This function is supposed to fill the array. This
could of course be done like this:

<code>

const int rank = 3;
unsigned long array[3];

libraryFunction(array);

</code>

In my case, it is important that the array can be dynamically allocated (to
be able to use another rank), so I do this:

<code>

unsigned long* array;
int rank = getRank();
array = new unsigned long [rank];

libraryFunction(array);

</code>

The problem here (in my opinion), that the array is not 'recognised'
anymore. In the first example array was still just that: an array. In the
second example, array is only a pointer to the first data member of the
array. Now, this should not make any difference, should it? All arrays are
just pointers to the first member, or not? If not, is there any method to
'cast' array to be of the array type? I mean something like this:

<code>

unsigned long* array;
int rank = getRank();
array = new unsigned long [rank];

libraryFunction( (unsigned long[]) array);

</code>

The above doesn't compile, but I'm looking for anything that does work....
Any help is appreciated.

Michiel



Sun, 11 Sep 2005 21:42:43 GMT  
 pointer to array not recognised as array?

Quote:

> Hi all,

> to be able to use a function of an existing library, i have to pass
> it an array of unsigned longs. This function is supposed to fill the
> array. This could of course be done like this:

> <code>

> const int rank = 3;
> unsigned long array[3];

> libraryFunction(array);

> </code>

> In my case, it is important that the array can be dynamically
> allocated (to be able to use another rank), so I do this:

> <code>

> unsigned long* array;
> int rank = getRank();
> array = new unsigned long [rank];

> libraryFunction(array);

> </code>

> The problem here (in my opinion), that the array is not 'recognised'
> anymore. In the first example array was still just that: an array. In
> the second example, array is only a pointer to the first data member
> of the array. Now, this should not make any difference, should it?
> All arrays are just pointers to the first member, or not? If not, is
> there any method to 'cast' array to be of the array type?

Arrays barely exist in C/C++.  In memory allocation terms, an array is not a
pointer to the first element - an array really is an array.  The _name_ of
the array, however, is synonymous with the address of the first element, and
hence behaves like a pointer whenever it's mentioned by name.  There is no
way to cast the pointer to an array type because array types don't exist in
C++.

- Show quoted text -

Quote:
> I mean
> something like this:

> <code>

> unsigned long* array;
> int rank = getRank();
> array = new unsigned long [rank];

> libraryFunction( (unsigned long[]) array);

> </code>

> The above doesn't compile, but I'm looking for anything that does
> work.... Any help is appreciated.

> Michiel

You're just describing C/C++'s crappy support for arrays - those are the
limitations that we live with.  What exactly are you trying to accomplish -
do you want to know the size of the array inside libraryFunction?  If so,
you have two choices:  pass the size as an additional parameter or use
something a bit more advanced than an array - for example, use a
std::vector<long> instead.

-cd



Sun, 11 Sep 2005 22:25:59 GMT  
 pointer to array not recognised as array?

Quote:

> You're just describing C/C++'s crappy support for arrays - those are the
> limitations that we live with.  What exactly are you trying to
accomplish -
> do you want to know the size of the array inside libraryFunction?  If so,
> you have two choices:  pass the size as an additional parameter or use
> something a bit more advanced than an array - for example, use a
> std::vector<long> instead.

> -cd

Carl, thanks for the answers. The problems are resolved.

Michiel



Mon, 12 Sep 2005 17:58:59 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Array of pointers, pointer to array...

2. array pointer/pointer array

3. arrays pointers array of pointers

4. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

5. pointers to an array and accesing elements of the array

6. array of pointers to array of structs

7. Copy Array string to another Pointer array string in ANSI-C

8. Array of pointers to arrays?

9. convert list to array, arrays of pointers (to strings)

10. 2D array of pointers to 2D arrays

11. declaring an array of pointers to malloced arrays

12. qsorting & managing struct arrays, pointer arrays

 

 
Powered by phpBB® Forum Software