# Pass arrays of data as & or *?

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

## Recommended Posts

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
//
{
// 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;
}



##### Share on other sites
Ew. No, prefer pointers when referring to arrays.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by xllx_relient_xllxMaybe 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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by SneftelPointers, 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?

##### Share on other sites
Quote:
 Original post by RaeldorSo I should use a pointer in this particular example instead of the pass-by-reference operator?
.... Have you read any of Sneftel's replies?

##### Share on other sites
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.

##### Share on other sites
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.

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631070
• Total Posts
2997746
×