Jump to content
  • Advertisement
Sign in to follow this  
DangerDave

Passing arrays to a function

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!