passing a struct array in a function as refence... ?

Started by
25 comments, last by Zahlman 16 years, 9 months ago

#include <iostream>
#include <string>

using namespace std;

struct COORD
{
	int x;
	int y;
};

void set_zero(COORD &n[])
{
	for(int i=0; i<5; i++)
	{
		n.x = 0;
		n.y = 0;
	}
}

void main(void)
{
	COORD c[5];

	for(int i=0; i<5; i++)
	{
		c.x = 10;
		c.y = 10;
	}

	set_zero(c);
}

error: error C2234: '<Unknown>' : arrays of references are illegal it seems that arrays of references are illegal... Then what is the possible way to pass an array of data with references or pointers? Thanks!
Advertisement
It's mainly an operator priority thing (and also, you're missing the array size: arrays in C have a fixed size that is embedded in the type).

void set_zero(int (&n)[5]) {}
thanks!

is there a recommended website where I could learn more about operator priority?
(me again)

mmmm even if the syntax is okay for the compilor, the values isn't returning from the parameter...

#include <iostream>#include <string>using namespace std;struct COORD{	int x;	int y;};void set_zero(COORD (&n)[5]){	for(int i=0; i<5; i++)	{		n.x = 0;		n.y = 0;	}}void main(void){	COORD c[5];	for(int i=0; i<5; i++)	{		c.x = 10;		c.y = 10;		cout << "c[" << i << "].x = " << c.x << endl;		cout << "c[" << i << "].y = " << c.x << endl << endl;	}	cout << "---------------------------------------------------------" << endl;	cout << "CALL FUNCTION TO SET ALL COORDS FROM ARRAY TO ZERO       " << endl;	cout << "---------------------------------------------------------" << endl;	set_zero(c);	// test to see if coords are really to zero.	for(i=0; i<5; i++)	{		c.x = 10;		c.y = 10;		cout << "c[" << i << "].x = " << c.x << endl;		cout << "c[" << i << "].y = " << c.x << endl << endl;	}}
http://www.cppreference.com/operator_precedence.html

here you can find the info you want.

Alternatively you could just pass a COORD* pointer. The name of an array is a pointer to it's first element, so you can do this:

#include <iostream>void print(int *array, int size){	std::cout << array[13] << std::endl;};int main(){	int array[14] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};		print(array, 14);	};



EDIT: you actually reset the values to 10 after calling set_zero and before reprinting them :P Careful when copy-pasting :P
In this example, there is no need to pass the array by reference. Don't forget that arrays in C are really *just* pointers (extra syntax non-withstanding), so any function passed an array can modify the array's contents.
You only need to pass a reference to an array if you need the function to change the array pointer itself (i.e. pointing it at another buffer), and that doesn't work for static arrays.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

anyone could take my code please and bring changes to it? would really help me to understand by seeing the difference...

EDIT: AHHHHHHHHHHHHHHHHHHH! I'm stupid! really stupid!!!

lol

I've reassigned the value to 10 again after calling the set_zero function.

it's what we call "danger of copy/pasting code" lol
Quote:Original post by swiftcoder
In this example, there is no need to pass the array by reference. Don't forget that arrays in C are really *just* pointers (extra syntax non-withstanding), so any function passed an array can modify the array's contents.
You only need to pass a reference to an array if you need the function to change the array pointer itself (i.e. pointing it at another buffer), and that doesn't work for static arrays.


Arrays have size information while pointers don't (you cannot determine how many elements a pointer can iterate through). Arrays decay to a pointer to their first element when evaluated in a pointer context, which loses their size information. Passing an array by reference conserves the type information. You cannot change what the array represents (an array is a sequence of elements, it doesn't point to one). Dynamic arrays are generally an incorrect or approximate denomination, the only arrays in C and C++ are static-sized ones, the rest is either pointer to buffers or container classes.
Quote:Original post by swiftcoder
In this example, there is no need to pass the array by reference. Don't forget that arrays in C are really *just* pointers (extra syntax non-withstanding), so any function passed an array can modify the array's contents.


Arrays decay to pointers, arrays are not pointers.

Quote:
You only need to pass a reference to an array if you need the function to change the array pointer itself (i.e. pointing it at another buffer), and that doesn't work for static arrays.


If you pass a reference to an array, you can use sizeof on the array and not get sizeof(pointer).

However the OP might find he is better served by using std::vector or boost::array.
Quote:Original post by rip-off
Quote:Original post by swiftcoder
In this example, there is no need to pass the array by reference. Don't forget that arrays in C are really *just* pointers (extra syntax non-withstanding), so any function passed an array can modify the array's contents.

Arrays decay to pointers, arrays are not pointers.

nitpicker :)
If I declare 'char A[5]', A is a pointer. Albeit to a either the static data section or to a region on the stack, and immutable.

Quote:
Quote:You only need to pass a reference to an array if you need the function to change the array pointer itself (i.e. pointing it at another buffer), and that doesn't work for static arrays.

If you pass a reference to an array, you can use sizeof on the array and not get sizeof(pointer).

But in fact that is just a syntactical issue, and you *have* created an unnecessary temporary pointer. And are you quite sure about that? I am not near a compiler to test that, but it seems to me you should get the same result whether you pass an array, or a reference to an array (note that I never mentioned using a pointer directly).

Quote:However the OP might find he is better served by using std::vector or boost::array.

Most likely.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement