Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using an array as an index...

Status
Not open for further replies.

SirLars

Technical User
Oct 24, 2000
47
CA
Ok, this may seem like a stupid question, but... it just doesn't make sense to me...
even when looking at the MSDN site and through my C++ textbooks...

here's the sample code i am having problems grasping the logic....

Code:
VARIANT _stdcall retVariantArray(void) 
{
   COleSafeArray saRet;         
   DWORD numElements[] = {10, 10}; // 10x10

   // Create the 2 dimensional safe-array of type VT_R8 with size 10x10
   saRet.Create(VT_R8, 2, numElements);

   // Initialize safearray  with values...
   long index[2];
   for(index[0]=0; index[0]<10; index[0]++)
   {
      for(index[1]=0; index[1]<10; index[1]++)
       {
         double val = index[0] + index[1]*10;
         //populate the safearray elements with double values
         saRet.PutElement(index, &val);
      }
   }
   // Return the safe-array encapsulated in a VARIANT...
   return saRet.Detach();
}

now... if index is an array.. there can be index[0] and index[1]... right?

and PutElement is a function to put a value (in this case &quot;val&quot;) into an array (in this case &quot;saRet&quot;)... right?

and the first value is to be the index of the saRet array... right?

so, how does this function determine whether the value of index[0] or the value of index[1] is the proper index for this function?

OR.. is it because saRet is a 2 dimensional array.. that BOTH values of index (index[0] and index[1]) are used to find a place in saRet?

thanks in advance...

Lars.
 
> BOTH values of index (index[0] and index[1]) are used to find a place in saRet

seems like that.
 
think of index[0] as i and index[1] as j.
For the 2-dimensional array you would have to pass i and j.
for a 5-dimensional array you would pass 5 indexes a.s.o.
But if you use index instead, you'll have to pass only ONE
variable, and PutElement accepts only ONE variable (which actually is a pointer). So every time you pass index, it addresses exactly ONE element of your target-array.
Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top