C和指针第十一章编程练习

11.11.1

void * my_calloc(size_t n, size_t m)
{
    void * temp = malloc(n * m);
    if(!temp)
        return NULL;
    return temp;
}

11.11.2

int * read(void)
{
    int count = 1;
    int * temp = (int *)malloc(sizeof(int)), value;
    puts("请输入一列整数:");
    while(scanf("%d", &value) && value != EOF)
    {
        *(temp + count - 1) = value;
        count++;
        realloc(temp, count * sizeof(int));
    }
    printf("%d ", count);
    int i;
    for(i = 0; i < count - 1; i++)
        printf("%d ", *(temp + i));
    return temp;
}

11.11.3

char * read(void)
{
    int count = 1;
    char ch;
    char * str = (char *)malloc(sizeof(char));
    puts("请输入字符串:");
    while((ch = getchar()) != '\n')
    {
        *(str + count - 1) = ch;
        count++;
        realloc(str, count * sizeof(char));
    }
    *(str + count - 1) = '\0';
    return str;
}

11.11.4
似乎多创建到一块内存? 到链表章节再仔细学习

#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
    int i;
    struct list * n;
}List;
int main(void)
{
    List l;
    List * head, * up, * down;
    l.i = 0;
    l.n = NULL;
    head = &l, down = &l;
    while(scanf("%d", &down->i))
    {
        down->n = (List *)malloc(sizeof(List));
        down = down->n;
        down->n = NULL;
    }
    up = head;
    while(up)
    {
        if(up->n)
            printf("%d\n", up->i);
        down = up->n;
        free(up);
        up = down;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值