0

I recently started programming with C and see code as shown below. I wonder, why does it have * at the start of the function?

int *inc(record *head, int key){
    record *tmp = head;
    while(tmp != NULL){
        if(tmp->key == key) {
            tmp->value = tmp->value + 1;
        }
        tmp = tmp->next;
    }
    return 0;
}

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    It returns a pointer to an int, which in this case is a nullptr. – Devolus Apr 08 '21 at 12:27
  • That `*` isn’t the start of the function; it’s the end of the return type. The function returns `int*`, i.e., it returns a pointer to `int`. – Pete Becker Apr 08 '21 at 13:06
  • You can write it as `int* inc(...)`, then it becomes more clear. However, there are reasons why it is common to write `int *inc(...)`, instead. E.g. you can declare variables like this: `int *x, y` which declares an `int *x` and an `int y`. However, `int* x, y` is visually not so clear as the declaration before. It mocks unattended audience to think it declares two int pointers. The other way round `int x, *y` works, too. – Wör Du Schnaffzig Apr 08 '21 at 13:19

0 Answers0