0
void max_min(int *a, int size, int *max, int *min){
    insertion_sort(a, size);
    *min = a[0];
    *max = a[size-1];
}

This C function works fine. I just get confused, when use the parameter,

  • you need to use *min to use the the pass-in parameter as a value in some address.
  • But when you treat *a as an array, you don't need to add the asterisk before that variable.

Why?

Albert Gao
  • 3,653
  • 6
  • 40
  • 69
  • You mean to ask, why don;t we write `*a[0]`, right? – Sourav Ghosh Jul 19 '16 at 09:59
  • There's a typo: `a[n-1]`, it should be `a[size-1]` instead. – rubikonx9 Jul 19 '16 at 09:59
  • What do you think `*(a+1)` means? Also, `n` is either undefined or global in that function. Make sure it's not zero, as it is by default. – Transcendental Jul 19 '16 at 09:59
  • @rubikonx9 Thanks! Fixed! – Albert Gao Jul 19 '16 at 10:00
  • Possible duplicate of [Pointers to pointers vs. normal pointers](http://stackoverflow.com/questions/38076981/pointers-to-pointers-vs-normal-pointers) – xenteros Jul 19 '16 at 10:01
  • @Transcendental If *a is a pointer type as a parameter in a function. Then inside that function, a should be the address. *a should be value of in that address, right? – Albert Gao Jul 19 '16 at 10:02
  • Good! Please go and read a prelim C book first then. Concentrate on array indexing and pointer arithmetic, specifically. – Sourav Ghosh Jul 19 '16 at 10:10
  • @AlbertGao Note that `a` is a pointer type, not `*a`. To get a bit more hang of what `*a` does and to differentiate between the value of a pointer and the value that pointer points to, take a look at the accepted answer at [this question](http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean) – Transcendental Jul 19 '16 at 14:44

2 Answers2

4

You can write a[0] as *a and a[size-1] is equivalent to *(a+(size-1)).

When passed to function as arguments pointers and arrays are equivalent(but only in this case). This subscript operator can be used for pointers, like -

a[5]  <==> *(a+5)

As a matter for fact a[5] is equivalent to 5[a] (as suggested by rubikonx9 )

You can read about the subscript operator here

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • 1
    Due to this mechanism, you can also write `5[a]`. Not really useful, I think, but a fun fact nonetheless. – rubikonx9 Jul 19 '16 at 10:11
  • @rubikonx9 Yes , it would be good to know for OP too. – ameyCU Jul 19 '16 at 10:12
  • 1
    Also, you can write `min[0]` instead of `*min`. Not useful either, but it shows that the compiler doesn't see any difference between pointers and arrays here. – anatolyg Jul 19 '16 at 10:13
  • @anatolyg I think first line would cover that :) – ameyCU Jul 19 '16 at 10:13
  • `a[size-1] is equivalent to *(a+size-1).` This is not correct. If this were true a lot of code would be broken. – 2501 Jul 19 '16 at 10:15
  • @2501 Can you explain more on it ? – ameyCU Jul 19 '16 at 10:18
  • What is the associativity of additive operators? How does that play a role with pointer addition and the array subscript operator. – 2501 Jul 19 '16 at 10:19
  • @2501 Are you suggesting that `a+5` would point to a memory beyond last element of array ? If you are but that should not cause any problem . – ameyCU Jul 19 '16 at 10:21
  • I'm talking about the specific quoted statement you made. I'm trying to not give out the answer directly. Take a look at how the [] operator is defined. – 2501 Jul 19 '16 at 10:22
  • @2501 Ok, `()` brackets is what you are talking about . – ameyCU Jul 19 '16 at 10:28
  • Yup. Without those the pointer could point out-of-bounds in otherwise valid code. – 2501 Jul 19 '16 at 10:31
  • @2501 Agreed , I would add those , but pointer pointing out of bounds in not a problem until it is not dereferenced . – ameyCU Jul 19 '16 at 10:31
  • Actually it is. Pointing beyond one-past-the-last element is ub. – 2501 Jul 19 '16 at 10:32
  • @2501 I don't think so . Pointing out of bound would not cause UB , untill and unless memory it points to is read or accessed to write . – ameyCU Jul 19 '16 at 10:33
  • Do you think so or know so? – 2501 Jul 19 '16 at 10:36
  • Anyway, I can back my claim: *C11 6.5.6 8: If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.* – 2501 Jul 19 '16 at 10:42
  • @2501 Yes but it would be reuired to be evaluated , if not then ? – ameyCU Jul 19 '16 at 10:45
  • It is. Every (sub)expression is evaluated. – 2501 Jul 19 '16 at 10:45
  • @2501 Ok , if you were to talk about `a+size` , then is this will cause UB ? Inside expression `*(a+size-1)`. – ameyCU Jul 19 '16 at 10:47
  • Probably not because it is probably pointing to one-past-the-last-element(which is ok), but we can't say for sure what is being passed into the function. – 2501 Jul 19 '16 at 10:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117686/discussion-between-ameycu-and-2501). – ameyCU Jul 19 '16 at 10:49
0

To answer your confuse

  • *min means the variable that pointer points to. If you want to change it's value,so you need to use *min(That's the same as the normal assignment). If you use the min = a[0],You only make the pointer min to point to another address(address's value is the a[0]). So the origin variable that min points to will not be changed. That may be dangerous.

  • the name of the array is the pointer point to the first element. so that likes the normal point does.

what's more,value inside the address &a[0] and address a are equal. Value in address &[0] is a[0] and value in address a is *a. Hence, arr[0] is equivalent to *arr.

so a represents the array, and the *a represents the value of array.

Hope that can help you.

Jesse Chen
  • 469
  • 5
  • 7