0

I'm implementing destroy method for a linked list by keeping pointers to previous and current nodes. I'd like to declare them as register variables in a single line. Is it possible?

void list_destroy(list *ls) {
  // does this mean both prev and curr are register variables?
  register node *prev, *curr;
  
  for (prev = 0, curr = ls; curr != 0; curr = curr->next) {
    if (prev != 0)
      free(prev);
    prev = curr;
  }
  free(ls);
}

or

void list_destroy(list *ls) {
  node register *prev, *curr;
  
  for (prev = 0, curr = ls; curr != 0; curr = curr->next) {
    if (prev != 0)
      free(prev);
    prev = curr;
  }
  free(ls);
}
3
  • Give up - write it in assembler. Mar 26, 2022 at 3:50
  • 1
    For the most part, you should forget that register is a keyword in C — just like you never use auto either. The compiler will make register assignments on it's own, better than you can, and it will largely if not completely ignore your use of the keyword. Mar 26, 2022 at 4:45
  • @JonathanLeffler Noted and thank you for your advice. I removed register from this function.
    – kovac
    Mar 26, 2022 at 6:32

1 Answer 1

1

register is a storage class specifier, so it applies to all the declarators in the declaration. It applies to bit prev and curr in these examples.

Commonly, you put storage class specifiers first on the line (before any type specifiers) but that is not required. Storage class and type specifiers can be in any order (but all must be before the declarators and any pointers or qualifiers or other modifiers on the declarator)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.