相关疑难解决方法(0)

如何找到'sizeof'(指向数组的指针)?

首先,这里是一些代码:

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个字节)?

c arrays pointers sizeof

288
推荐指数
8
解决办法
35万
查看次数

为什么malloc在我的c程序中不起作用?

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位,我使用的是代码块.

c malloc sizeof

-2
推荐指数
1
解决办法
206
查看次数

标签 统计

c ×2

sizeof ×2

arrays ×1

malloc ×1

pointers ×1