Sign in to follow this  

Passing 2D array by reference

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the syntax for passing a 2D array (static) by reference? I tried the following but it seems to be passing by value. int a[][2] = { { 1, 2 }, { 3, 4 } }; void foo(int param[][2]) {} foo(a); And void foo(int ¶m[][2]) {} doesn't work...

Share this post


Link to post
Share on other sites
#include <stdio.h>

int a[][2] = { { 1, 2 }, { 3, 4 } };

void foo(int param[][2]) {
param[0][0] = 5;
}

int main(){
foo(a);
printf("%d\n", a[0][0]);
}

It should print 5, so it's definitely not passing by value. In C static arrays are implemented using pointers, so they are passed by pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathkrush
#include <stdio.h>

int a[][2] = { { 1, 2 }, { 3, 4 } };

void foo(int param[][2]) {
param[0][0] = 5;
}

int main(){
foo(a);
printf("%d\n", a[0][0]);
}

It should print 5, so it's definitely not passing by value. In C static arrays are implemented using pointers, so they are passed by pointer.


Oh... You're right. I have no idea why it didn't work for me.

But this brings another question:
How do I pass it by value then?

Share this post


Link to post
Share on other sites
I'm pretty much a novice, but I would suggest to pass by value:

foo(&a)

Again, i'm a novice. However, it seems to me that you want to pass the entire array to the function. If that is the case, I would suggest a wrapper. If you can't do a wrapper, try this:

#include <stdio.h>

int a[][2] = { { 1, 2 }, { 3, 4 } };

void foo(int *param[][2]) {
param[0][0] = 5;
}

int main(){
foo(&a);
printf("%d\n", a[0][0]);
}

Let me know how it turns out ... I haven't actually tried the above method, but it might actually work.


Share this post


Link to post
Share on other sites
Quote:
Original post by argonaut
I'm pretty much a novice, but I would suggest to pass by value:

foo(&a)

Again, i'm a novice. However, it seems to me that you want to pass the entire array to the function. If that is the case, I would suggest a wrapper. If you can't do a wrapper, try this:

#include <stdio.h>

int a[][2] = { { 1, 2 }, { 3, 4 } };

void foo(int *param[][2]) {
param[0][0] = 5;
}

int main(){
foo(&a);
printf("%d\n", a[0][0]);
}

Let me know how it turns out ... I haven't actually tried the above method, but it might actually work.


Ok, you're confusing me. Why would foo(&a) be passing by value? It seems more like passing "the reference of the pointer to the array."

I haven't had the chance to test it yet, but it just look kinda odd to me.

Share this post


Link to post
Share on other sites
You cannot pass c-style arrays by value. You can either make them const to prevent accidental manipulation, or make them in a small structure and pass that by value.

Or just use std::vector for all you array-like needs.

If you have boost, try boost::array.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
You cannot pass c-style arrays by value. You can either make them const to prevent accidental manipulation, or make them in a small structure and pass that by value.

Or just use std::vector for all you array-like needs.

If you have boost, try boost::array.


Thanks for the clarification.

Share this post


Link to post
Share on other sites
You can pass them by reference to keep sizing information:

template< class T, size_t D1, size_t D2 >
void Do( T ( &arr )[D1][D2] ) {
arr[D1 / 2][D2 / 2] = 1;
}


int main() {
int arr[4][6] = { 0 };
Do( arr );
}



Although, you would be better off using std::vector, boost::array or boost::multi_array, as rip-off mentioned.

Share this post


Link to post
Share on other sites
Here is how I would approach passing by value:


#include <iostream>

template <class T, int s>
class Array
{
T array[s];

public:

Array(const T t[s])
{
for(int i=0; i<s; i++)
array[i] = t[i];
}

Array(const Array& t)
{
for(int i=0; i<s; i++)
array[i] = t[i];
}

T& operator[](int i)
{
return array[i];
}

const T& operator[](int i) const
{
return array[i];
}
};

template <class T, int s>
std::ostream& operator<<(std::ostream& str, const Array<T,s>& a)
{
for(int i=0; i<4; i++)
str << a[i] << " ";

return str;
}

//An equivalent of memset, passes arrays by value
template <class T, int s>
void MemSetByVal(Array<T, s> a, T v)
{
for(int i=0; i<s; i++)
a[i] = v;
return;
}

//An equivalent of memset, passes arrays by reference
template <class T, int s>
void MemSetByRef(Array<T, s> &a, T v)
{
for(int i=0; i<s; i++)
a[i] = v;
return;
}

int main()
{
int arrayInit[] = {1, 2, 3, 4};
Array<int, 4> a = arrayInit;

MemSetByVal(a, 0);

std::cout << a << "\n";

MemSetByRef(a, 0);

std::cout << a << "\n";
}




It should print
1 2 3 4
0 0 0 0

because MemSetByVal doesn't do anything to the array, but MemSetByRef does. boost::array already does all of that, but if you don't want to use boost, there is nothing wrong with writing a custom array wrapper, at least you will get to practice writing templates.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this