staticグローバル変数は同一ファイル内でしか参照されないシングルトンな変数だから、マルチスレッドプログラミングで使っても大丈夫さ!とは作者の言葉。 実際にはもっとたくさんstaticグローバルな変数が宣言されてて、何ファイルにも渡ってどんどん参照&更新されまくってたけど…むしろよくやるなぁ。
//============ File : head.h ============
#include <stdio.h>
#include <string.h>
extern void other_func(char *_buf);
//============ File : main.c ============
#include "head.h"
static char buf[1024] = "";
int main(int argc, char *argv[])
{
strncpy(buf,"unko",4);
other_func(buf);
printf("%s¥n", buf); // => "unkode¥n"
return 0;
}
//============ File : other_func.c ============
#include "head.h"
void other_func(char *_buf)
{
strncat(_buf,"de",2);
}
このサンプルではstaticの悪用はまるで説明できていないんじゃ? むしろ文字列関数の使い方が問題。