-2

Let's say we have afunction like.

void f(int *k){
  k++; //increments the pointer to the second element
  (--(*k)); //what exactly does is mean? 

}
int main(){
  v[]={1,2,3};
  f(v); //passes the pointer to the forst elemento of v
}

What order does it follow? And what if I had --k[1] in a function like void f(int k[3])?

bog
  • 1,323
  • 5
  • 22
  • 34

2 Answers2

1

c++ differencebetween --k[i] and k[i]--

Former does pre-decrement. Latter does post-decrement.

(--(*k)); //what exactly does is mean?

operator* is the dereference operator.

What order does it follow?

All expressions follow the operator precedence rules. Parenthesized groups have tighter binding than any operator.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Best idea would be to open debugger and check step by step:)

When you call function f(v) you pass pointer to array - to the first element of it.

Inside of f(v) you first increase pointer by 1 (k++). What's to note here, it doesn't matter what type of objects you pass, it's always moved to another object, so size of object doesn't matter.

Now (--(*k)) - you decrement what's inside of *k (which now points to the second element of array - so 2).

In conclusion, after you finish function v == { 1, 1, 3};

MaciekGrynda
  • 583
  • 2
  • 13