Safari
Safari

Reputation: 11935

C array and pointer to array

I have a simple question.. I have a C program.. I have a array of long and I would like to to pass a pointer to this array into two function. Is correct to pass the array in this way?

long[] myArray


void myFunction1(long *myArray[]){

*myArray[0] = 1;

}

void myFunction2(long *myArray[]){

*myArray[1] = 2;

}

Is correct this?

Upvotes: 2

Views: 140

Answers (3)

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

Nope, this is not correct because when you declare a variable like this -

long *myArray[]

it represents an array which can hold long pointers, not long values. I am guessing (from your function body) this is not what you intend to do.

Change your function parameter to long myArray[], and assign value without the asterisk.

Like this -

void myFunction1(long myArray[]){
    myArray[0] = 1;
}

void myFunction2(long myArray[]) {
    myArray[1] = 2;
}

Or, if you like to use pointer, then -

void myFunction1(long *myArray){
    myArray[0] = 1;
}

void myFunction2(long *myArray) {
    myArray[1] = 2;
}

In both of the cases, remember to ensure that the size of your array is at least 2.

Upvotes: 2

Eric Postpischil
Eric Postpischil

Reputation: 222754

When you declare void myFunction1(long *myArray[]), I suspect you are making two mistakes.

One is that I think you intended this to be a pointer to the array. However, the brackets, [], bind more tightly to the identifier, myArray, than the asterisk, *, does. So the parameter is long *(myArray[]), which is an array of pointers to long, but I think you intended a pointer to an array of long, which would be long (*myArray)[].

You actually could declare the function with this parameter, pass it a pointer to the array, as with myFunction1(&myArray), and use the pointer inside the function, as with (*myArray)[0] = 1;.

However, C gives us a shortcut, and not using that is the second mistake. If you declare the parameter as long myArray[], then it looks like an array but it is actually converted to long *myArray, which is a pointer to long. Then you can pass the array as myFunction1(myArray). Although myArray is an array, C converts it to a pointer to the first element. So the argument myArray will match the parameter long myArray[]. Then, inside the function, you can use the pointer with myArray[0] = 1;.

The shortcut results in shorter notation and is generally considered to be more natural notation, so it is preferred.

Upvotes: 1

Mike
Mike

Reputation: 49403

Well, let's think about it:

long myArray[3]; // This is an array of 3 long's

long *myArray[3]; // Is this? No, this is an array of 3 pointers to longs.

That pretty much tells you the answer.

void myFunction1(long *myArray[]){

This is not how you pass an array to a function (long or otherwise). You need to take just an array:

void myFunction1(long myArray[]){
    myArray[0] = 1;

or you can take a pointer:

void myFunction1(long *myArray){
    myArray[0] = 1;

That works because arrays decay to pointers.

Upvotes: 2

Related Questions