简体   繁体   中英

Pass an array by reference into function argument

I want to manipulate an array of integers in a function, but I am not sure if I should use the address prefix or the pointer prefix:

void shuffle(int *deck[]){  
    //statements
}

or

void shuffle(int &deck[]){  
    //statements
}

Further, when I directly want to manipulate the values of the array, I'm not sure if I should use (within the function)

*deck[4] = 34

or something else.

Any clarification is appreciated.

There are no references in C( your Q is tagged C only ) so you will have to use the pointer version.

When you say pass an array to the function, what you essentially pass to the function is the pointer to its first element, both of the following two syntaxes mean one and the same thing to the compiler:

void shuffle(int *p_deck); 
void shuffle(int deck[]);

C doesn't have references (a) , that's C++.

In C, arrays of X decay into a pointer to X[0] when passed to functions, so one way is to use:

void shuffle (int *pDeck) {
    // use pDeck[something]
}
:
int deck[] = {0,1,2,3,4,5,6,7,8,9};
shuffle (deck);

I actually prefer that method to the void shuffle (int pDeck[]) variant since the former makes it absolutely clear that you're now dealing with a pointer rather than an array.

The reason this is important is because you lose the size information when you do that, so you may want to pass that in as well:

void shuffle (int *pDeck, size_t sz) {
    // use pDeck[0 thru sz-1]
}
:
int deck[] = {0,1,2,3,4,5,6,7,8,9};
shuffle (deck, sizeof (deck) / sizeof (*deck));

(a) : Although they are a very nice feature. I do hope that ISO considers them for the next C standard, since a large number of problems newcomers to the language have are involved with pointers, and references can hide the complexity of that very well.

Arrays decay into pointers when passed to functions, so there's no point in using an additional level of indirection here.

According to §6.3.2.1 of the C99 standard,

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type " is converted to an expression with type "pointer to type " that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

void shuffle(int deck[]) {
  ...
}

Note that C does not support references, only pointers. You're probably thinking of C++.

You would have to use the pointer version, but because of the Right-Left Rule, I believe your:

int * deck[] would actually be an array of int pointers, instead of an array of ints.

http://cseweb.ucsd.edu/~ricko/CSE131/rt_lt.rule.html

You should just pass a reference to the first element in the array:

int * deck but you may want to pass in the size of the array so you avoid going out of bounds.

Neither.

Since arrays can be passed by reference only, you don't need to do tricks, just pass a pointer and dereference it. (That syntax involving the & in your 2nd function is not valid, anyway). So:

void shuffle(int arr[])
{
    arr[0] = 1337;
}

or

void shuffle(int *arr)
{
}

etc. And you can pass it like this:

int deck[52];
shuffle(deck);

You can pass an array by reference in C++, if you specify the size of the array:

void shuffle(int (&deck)[52])
{
   deck[4] = 34;
}

If you don't know the size, or if you're limited to C, you need to use pointers.

The other (C++) option is to use a vector for the deck and pass a reference to the vector instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM