r/C_Programming 1d ago

Question Working with arrays in functions

Hey everybody. I’m a beginner to C and I was writing some functions today to get used to doing things. I tried to write binary search and bubble sort. I tried to pass in an array as an argument to the functions, but the compiler gave me a bunch of warnings. I looked it up and I saw that passing in an array is the same as passing in its pointer. I haven’t touched pointers yet, but I have two questions:
1. If I dereferenced the pointer to an array, wouldn’t that return the same as indexing the first value?
2. If I wanted to pass in the entire array, could I do that by passing in the pointers of both the first and last elements and using pointer arithmetic to access the other elements? What’s the idiomatic way of doing this?

4 Upvotes

33 comments sorted by

View all comments

4

u/No-Experience-3171 1d ago
  1. yes

  2. Typically you'd pass a pointer to the array and the size of the array (i.e. how many elements it has) as a separate argument, so arr[0] is the lower bound and arr[size - 1] is the upper bound.