1

I can't create a reference to an unsigned char array z within myfunc(). I've tried several ways but just can't figure it out. This is my most recent attempt:

int myfunc (int &y, unsigned char (&z)[2]);

int main(int argc, char *argv[]) {
 int y = 1;
 unsigned char z[2];

 int x = myfunc(y, z[2]);
}

int myfunc(int &y, unsigned char (&z)[2]) {
 y = 123;
 z[0] = foo;
 z[1] = bar;
 return 456;
}

I am getting the following errors:

error: in passing argument 2 of ‘int myfunc(int&, unsigned char (&)[2])’

For line int myfunc (int &y, unsigned char (&z)[2]);

And:

error: invalid initialization of reference of type ‘unsigned char (&)[2]’ from expression of type ‘unsigned char’

For line int x = myfunc(y, x);

How should I be referencing the array it self (and not an array of references?) ?

3
  • Using a vector makes this really easy, and is much better than raw arrays otherwise. Anyways, you're calling your function by passing a single element in. Use z, not z[2]. Commented May 3, 2012 at 19:50
  • I am trying to learn vectors along side this, how would that make this easier? The only way I can see to make this work is to make the array globally defined which as I understand it is not good. Commented May 3, 2012 at 19:52
  • 1
    With a vector, your signature would be: int myfunc (int &y, std::vector<unsigned char> &z). They provide syntax you'd expect from using other types of parameters. Commented May 3, 2012 at 19:56

3 Answers 3

10
myfunc(y, z[2]);

The expression z[2] is of type unsigned char (and refers to an element beyond the end of the array, so it is undefined behavior). You want to do:

myfunc(y, z);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you for the speedy response :)
2

call should be: int x = myfunc(y, z);

This is because z represents an unsigned char [2] but z[2] represents type char. Also z[2] accesses array out of bounds.

1 Comment

Thanks to you also for the speedy response :)
1

I'm a little confused about what you're trying to do. IMHO your signature should just be int myfunc (int &y, unsigned char z[2]).

Arrays don't get passed "by value" (if you want to do that, then using a vector is the easiest way), and the default makes your get_mac function (which I assume was supposed to be myfunc) work as desired (unless I've misunderstood your desire).

The signature int myfunc (int &y, unsigned char (&z)[2]) suggests that you might want to change the value of z itself in the caller (not just elements of z), but even the caller can't do that (!), and you'll get an error if you try. If you want that, use an actual pointer.

There is a legitimate use for passing the array by reference using the syntax in your example, which is to enforce the declared array size. But if you're just learning, I don't think you need to worry about this yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.