タグ

styleとcに関するmasterqのブックマーク (2)

  • software that sucks less | suckless.org software that sucks less

    Style Note that the following are guidelines and the most important aspect of style is consistency. Strive to keep your style consistent with the project on which you are working. It is up to the project maintainer to take some liberty in the style guidelines. Recommended Reading The following contain good information, some of which is repeated below, some of which is contradicted below. https://m

  • ARR01-C. 配列のサイズを求めるときに sizeof 演算子をポインタに適用しない

    ARR01-C. 配列のサイズを求めるときに sizeof 演算子をポインタに適用しない sizeof 演算子は、オペランドのサイズ(バイト単位)を求める。オペランドは、式または括弧で囲まれた型の名前のいずれかである。sizeof 演算子を使って配列のサイズを計算すると、コーディングエラーとなりやすい。 違反コード 以下の違反コード例では、関数 clear() が配列の要素を 0 で初期化する。この関数は、int array[] として宣言された引数を 1 つ取り、12 個の int からなる静的配列が渡されている。関数 clear() は sizeof(array) / sizeof(array[0]) という構文を使用して配列内の要素数を求めている。しかし、array は引数なので、ポインタ型となる。したがって、sizeof(array) は sizeof(int *) に等しくなる。

    ARR01-C. 配列のサイズを求めるときに sizeof 演算子をポインタに適用しない
    masterq
    masterq 2017/05/20
    "sizeof 演算子を使って配列のサイズを計算すると、コーディングエラーとなりやすい。", "構文 sizeof(array) / sizeof(array[0]) が成立するのは、array の定義が同じスコープ内にある場合である。"
  • 1