0

It seems like if I have a fixed-size array in C I can pass it to a function as long as the argument of the function is either a pointer or an array with no specified size, e.g.:

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

void fn1(int *a);
void fn2(int a[]);

I also noticed that I can use standard member access and pointer arithmetic with both.

Is there any formal or functional difference between the two approaches? Logically I would think that the second one is better because it indicates that I am supposed to treat the input argument as an array, but that's just my guess.

Any hints will be appreciated.

4
  • int a[] in the function parameter list is just another way to write int *a. Thus there are no different approaches to compare and contrast. The two notations mean exactly the same thing, by definition. Commented Sep 12, 2020 at 17:16
  • a[] => *a conversion is done at compile time. Like s->a => (*s).a Commented Sep 12, 2020 at 17:16
  • In that case, is there any better formal approach or convention? Commented Sep 12, 2020 at 17:21
  • 1
    @user3758232 use int a[] because it is easily understandable/readable. But there is not difference between int a[] and int *p Commented Sep 12, 2020 at 17:23

1 Answer 1

2

Is there any formal or functional difference between the two approaches?

There is no formal or functional difference between those two approaches.

During compilation int a[] is converted to int *a. But for better readability use int a[] over int *a.

print (a[2]) is converted to print (*a + (2 * sizeof(int))).

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

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.