如何在c中一行声明多个寄存器变量?

发布于 2025-01-17 01:07:20 字数 579 浏览 2 评论 0原文

我正在通过保留指向先前和当前节点的指针来实现链表的销毁方法。我想在一行中将它们声明为寄存器变量。是否可以?

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

或者

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2025-01-24 01:07:20

register 是一个存储类说明符,因此它适用于声明中的所有声明符。它适用于这些示例中的位 prevcurr

通常,您将存储类说明符放在行的前面(在任何类型说明符之前),但这不是必需的。存储类和类型说明符可以按任意顺序排列(但全部必须位于声明符以及声明符上的任何指针或限定符或其他修饰符之前)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文