Pass arrays of data as & or *?

Started by
9 comments, last by Raeldor 18 years, 10 months ago
Hi, I have a function that has a parameter that receives an array of data. Should I still declare this using the & format and then de-reference it as shown below, or is it preferred style to pass it as a pointer? Thanks

//
// build tree from input data which is an array the size of this quad
//
void QuadCompress::BuildTree(char &in_data)
{
	// loop through data to see if it is all the same
	int width=(int)m_topRight.x-(int)m_topLeft.x;
	bool isSame=true;
	m_data=(&in_data)[((int)m_topLeft.y*width)+(int)m_topLeft.x];
	for (int y=(int)m_topLeft.y; y < (int)m_bottomLeft.y && isSame; y++)
		for (int x=(int)m_topLeft.x; x < (int)m_topRight.x && isSame; x++)
			if ((&in_data)[(y*width)+x] != m_data)
				isSame=false;
}

Advertisement
Ew. No, prefer pointers when referring to arrays.
Maybe this can help.

Although this link does'nt pertain to C++. I think it's safely interchangeable with C++, given C++ is just a superset of C. If I'm wrong, Please feel free to correct me.

relient.
Quote:Original post by xllx_relient_xllx
Maybe this can help.

Although this link does'nt pertain to C++. I think it's safely interchangeable with C++, given C++ is just a superset of C. If I'm wrong, Please feel free to correct me.

relient.


& as a parameter definition is not available in C as far as I know. I think it was added to C++ as a way to pass safe pointers. I know it is the preferred way for regular pass-by-reference parameters, but not sure about arrays.
Quote:Original post by Raeldor
& as a parameter definition is not available in C as far as I know. I think it was added to C++ as a way to pass safe pointers. I know it is the preferred way for regular pass-by-reference parameters, but not sure about arrays.


Yeah, You're right. I somehow thought you were talking about using [] or *.
My bad. I guess it's one of those brain-farts - oops =).. Time to get some good night sleep.

Pointers, in C, were a few things. Among them:

1. A way to refer to blocks of memory (i.e. arrays)
2. A way to do pass-by-reference (to avoid copying data, or to allow a parameter to be output to)

In C++, the second use has been replaced with references. Not the first.
Quote:Original post by Sneftel
Pointers, in C, were a few things. Among them:

1. A way to refer to blocks of memory (i.e. arrays)
2. A way to do pass-by-reference (to avoid copying data, or to allow a parameter to be output to)

In C++, the second use has been replaced with references. Not the first.


So I should use a pointer in this particular example instead of the pass-by-reference operator?
Quote:Original post by Raeldor
So I should use a pointer in this particular example instead of the pass-by-reference operator?
.... Have you read any of Sneftel's replies?
Use a pointer, because a reference is always supposed to point to a valid object of its reference type. While you can argue that raw char data is always valid, I guess the object from conversion is not. Whenever you will need to do conversions it makes more sense to use pointers. Even if I convert a reference of type &CCar to a &CHouse but do not change the actual value, it doesn't implicitly mean the reference is now pointing to a valid CHouse object. Pointers that have a more "loose" definition should be used whenever there is no strict cohesion.
Perhaps as a suggestion even as overbloated as this may seem, you might consider encapsulating structures like this in classes and passing them as references, so rather than passing char** or char* to a function, use a container of some sort and pass the reference to the container. Avoid where
possible the use of pointers in cases where you expect an object to be passed
so that at least for the most part you don't have to worry about invalid
referencing to objects.

This topic is closed to new replies.

Advertisement