首先,这里是一些代码:
int main()
{
int days[] = {1,2,3,4,5};
int *ptr = days;
printf("%u\n", sizeof(days));
printf("%u\n", sizeof(ptr));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法找出ptr指向的数组的大小(而不是仅仅给出它的大小,在32位系统上是4个字节)?
struct BOOK
{
char name[120];
char author[120];
int year[50];
};
int main (void)
{
int i;
int number;
struct BOOK* books;
number = 50000;
printf("before \nsizeofbooks %d \n sizeofBOOK %d\n",
sizeof(books), sizeof(struct BOOK));
books = (struct BOOK*)malloc(sizeof(struct BOOK) * number);
printf("sizeofbooks %d \n sizeofBOOK %d\n",
sizeof(books), sizeof(struct BOOK));
free(books);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
before
sizeofbooks 4
sizeofBOOK 440
after
sizeofbooks 4
sizeofBOOK 440
Run Code Online (Sandbox Code Playgroud)
它总是输出4,即使我写入不同的数组,但我希望它会改变.我究竟做错了什么?
我的操作系统是winxp 32位,我使用的是代码块.