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;
}