C 語言中的 restrict 關鍵字


在本文中,我們將瞭解 C 語言中的 restrict 關鍵字。restrict 關鍵字最早出現在 C99 版本中。讓我們看看這個 restrict 關鍵字到底是什麼。

  • restrict 關鍵字用於指標宣告,作為指標的型別說明符。

  • 這個關鍵字不會新增新的功能。使用這個關鍵字,程式設計師可以告知編譯器可以進行的最佳化。

  • 當 restrict 關鍵字與指標 p 一起使用時,它告訴編譯器,ptr 是訪問此指標所指物件的唯一方式。因此,編譯器不會新增任何額外的檢查。

  • 如果程式設計師使用 restrict 關鍵字然後違反上述條件,它將生成一些未定義的行為。

示例

#include <stdio.h>
void my_function(int* x, int* y, int* restrict z) {
   *x += *z;
   *y += *z;
}
main(void) {
   int x = 10, y = 20, z = 30;
   my_function(&x, &y, &z);
   printf("%d %d %d", x, y, z);
}

輸出

40 50 30

更新於: 30-7-2019

1K+ 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始
廣告