Passing arrays to a function

Started by
4 comments, last by felisandria 19 years, 6 months ago
I have a static array in a class and I want to be able to change the reference so that it points to another array passed via a function, i.e. so that several instances of a class can operate on the same array.

(In the header)
float m_pfStateActionValueTable[NUM_STATES][NUM_ACTIONS];

(In the source)
void TermiteBrain::setStateActionValueTable (float** fStateActionValueTable){
	m_pfStateActionValueTable = fStateActionValueTable;
}

I get the error: error C2440: '=' : cannot convert from 'float ** ' to 'float [1024][14]' There are no conversions to array types, although there are conversions to references or pointers to arrays What am I doing wrong? I'd rather not store the array as dynamic, but if thats the only way then I suppose I'll have to.
Dave.
Advertisement
Have you considered using memcpy?

When you declare
float m_pfStateActionValueTable[NUM_STATES][NUM_ACTIONS];
you're allocating that memory block of the size you requested. Really if you want to change your global variable to point to some array, you need to make it a pointer in the first place, or you need to copy it in, but you really shouldn't take a non-pointer-declared variable like that and rip it away from the memory it was originally allocated to. C won't let you do that very easily for your own safety.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Ah, you misunderstand, I dont want to copy the array, I want several objects to be able to operate on the same array, so I need a reference rather than a copy.
Dave.
use float*
Quote:Original post by DangerDave
Ah, you misunderstand, I dont want to copy the array, I want several objects to be able to operate on the same array, so I need a reference rather than a copy.


well you can't make the name of a static array refer to a different array.
I understand what you want to do. I just think that's a bad idea given your declaration of m_pfStateActionValueTable. I went ahead and edited an explanation of that in my original reply (prior to your comment).

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

This topic is closed to new replies.

Advertisement