2

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?

7
  • 7
    Nope, this is not correct. Change your function parameter to long myArray[], and assign value without the asterisk. Commented May 8, 2013 at 17:07
  • log *array[] can be multidimensional array, an array is a pointer. *myarray[1] = 2 is like you dereference an element and access a subelement *myarray[1] = myarray[0][1] Commented May 8, 2013 at 17:07
  • 3
    Why don't you test it out and see if it works Commented May 8, 2013 at 17:07
  • Why do you want to do this? Since Arrays are already pointers their content is already passed as a reference (as opposed to as value). So any modifications inside the function will persist after the function call ends. Commented May 8, 2013 at 17:16
  • 1
    Read section 6 of the comp.lang.c FAQ. It explains the often confusing relationship between C arrays and pointers better than anything else I've read. Commented May 8, 2013 at 17:48

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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.

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.