#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *str;
char temp[128];
int size;
printf("array size = ");
fgets(temp, sizeof(temp), stdin);
size = atol(temp);
str = malloc(size);
printf("sizeof(str) = %d\n", (int) sizeof(str));
printf("sizeof(temp) = %d\n", (int) sizeof(temp));
/* fgets(str, sizeof(str), stdin); */
fgets(str, size, stdin);
printf("str[] = %s\n", str);
return 0;
}
array size = 14
sizeof(str) = 8
sizeof(temp) = 128
hello,world
str[] = hello,world
本当はコメントアウトしている fgets(str, sizeof(str), stdin); でstdinから文字列を受け取りたかったのですが
そうするとstr[] = hello,w で途切れてしまいました。
sizeofをprintfで確認すると普通に定義した文字列tempは128, mallocで取得したstrは8になりました。
8バイトというと、ポインタ変数のサイズなのだと思います。
ではsizeofでmallocで取得したアドレスサイズを取得するにはどう書けばできますか?
そのままsizeを書いてもプログラムは動くと思うのですが、sizeofで調べる方法が知りたいです。