Home > Enterprise >  How to declare multiple register variables in one line in c?
How to declare multiple register variables in one line in c?

Time:03-26

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);
}

CodePudding user response:

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)

  • Related