問題空間から警告:ポインタを整数
こんにちはGuysは、
はなぜ以下のコンパイルエラーを与えるプロトタイプ機能char *zalloc();
のコメントでしょうか?
プロトタイプ関数のコメントが削除されると、すべてうまく動作しているようです。
例コード:
#include <stdio.h>
#define ALLOCSIZE 10000
static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf; // allocbuf = &allocbuf[0]
char *fn();
// char *zalloc();
int main()
{
int a = 100;
char *c = fn();
char *d = zalloc(1000000);
printf("%s\n", c);
printf("%p\n", d);
}
char *zalloc(int n)
{
if (allocbuf + ALLOCSIZE - allocp >= n) {
allocp += n;
return allocp - n;
} else
return 0;
}
void afree(char *p)
{
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
char *fn()
{
return "foo";
}
コンパイラエラー:
example_24.c: In function 'main':
example_24.c:16:12: warning: initialization makes pointer from integer without a cast
char *d = zalloc(1000000);
^
example_24.c: At top level:
example_24.c:22:7: error: conflicting types for 'zalloc'
char *zalloc(int n)
^
example_24.c:16:12: note: previous implicit declaration of 'zalloc' was here
char *d = zalloc(1000000);
最近のCでは、分かりにくいメッセージは全く異なるはずです:宣言されていない関数 'zalloc'を呼び出してください。宣言されていない関数の呼び出しはCでは不正です。あなたのコンパイラは、言語仕様の要件に正式に従うように設定されていません。 – AnT
@alk申し訳ありません、私の悪い、私はちょっと新しい 'c'なので、ポインタを返す' char * zalloc(int n) 'は'関数ポインタ 'でした。 )、 '関数ポインタ'は関数を指すポインタのようです。 =) – dud3