Advertisement

Passing an array of references

Started by August 25, 2000 03:13 AM
3 comments, last by Void 24 years, 4 months ago
Isn''t it supposed to be the same as passing in the array buffer itself? What did I do wrong?? ..
    
void Test(float *f)	// temp function that prints the value

{
	int index;

	cout << "From H" << endl;
	for (index = 0; index < 10; index++)
		cout << f[index] << " ";
	cout << endl;
}

void main()
{

	int		index;		// looping var


	float	*array;
	array	= new float[10];		// a temp buf


	float **ref = NULL;				// the reference


	for (index = 0; index < 10; index++)	// initialize the buffer

		array[index] = (float)10 + index;

	ref = new float*[10];			// intitialize the references


	for (index = 0; index < 10; index++)	// set the reference in rev order

		ref[index] = &array[9 - index];

//	Test(array);		// this is ok

	Test(*ref);		// but this fails


	cout << "From main" << endl;
	for (index = 0; index < 10; index++)
		cout << *ref[index] << " ";
	cout << endl;

	delete	[]ref;
	delete	[]array;
}
    
Ok time to put the "head-wrecking pointer" thinking-cap on.
First off , your topic is wrong.
What your function is expecting is not an array of references, but a reference to an array (which is of course a pointer anyway, but I digress).

The first one array, is a reference to an array. SO your function accepts this fine.

What happens with the second is that you pass a reference to an array of references. The function prints out the first number because it is at the base address of *ref. but *ref isn''t equal to array so when it increments the pointer using the index it points to a completely different area of memory.

I''ll email you a diagram of what''s happening in memory if you like....
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Advertisement
Woo.. That is head wrecking indeed. Thanks for the explanation there.

So is there anyway to keep the Func() unchanged but passing a ref to an array of references?..

I need to build a smaller array from the float array buffer which to pass to Func() fbut without the copying of the array elements. (eg. say I want to pass like say elements from 0 - 5 of the original array only but without allocating another float block)

Is that possible?
quote: I need to build a smaller array from the float array buffer which to pass to Func() fbut without the copying of the array elements. (eg. say I want to pass like say elements from 0 - 5 of the original array only but without allocating another float block)


Just pass a pointer to the beginning of the array, and the number of elements if that''s necessary. Only the pointer is copied, ie. 4 bytes, not the whole array.

-Jussi
quote: Original post by Selkrank
Just pass a pointer to the beginning of the array, and the number of elements if that''s necessary. Only the pointer is copied, ie. 4 bytes, not the whole array.
-Jussi


That would only work if the smaller array is sequential which will not be the case with mine.

Anyway I think I can avoid this ref to array of references. I''m thinking way too hard..

This topic is closed to new replies.

Advertisement