让代码飞_动态内存分配函数解决动态数组问题_第四版
我写的是一个,不限制长度和个数的字符串输入/输出函数。动态数组一直是一个头疼的问题,相信大家也遇到过。大家可以拿过去测试一下。
程序代码: /***************************************************************************
* *
* File : strings.c *
* *
* Purpose : Unlimited string's count and string's size input. *
* *
* Author : Dennis Date : 9/6/2015 *
* *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "unli_str.h"
int main(void)
{
//variable declare
string* first = NULL;
string* current = NULL;
string* previous = NULL;
int buf_len = buffer_len;
int count= 0;
printf("input something you want : \n");
do{
current = sturct_memory(sizeof(string));
if(first == NULL)
first = current;
if(previous != NULL)
previous->next = current;
current->start = current->end = memory_init(buf_len * sizeof(char));
current = get_string(buf_len, current);
if(*(current->start) == '\n' && count == 0)
{
printf("you don't input anything\n");
break;
}
*(current->end - 1) = '\0';
current->str_length = strlen(current->start);//?strlen_s() 不支持动态字符串
store_strings(current);
count++;
current->next = NULL;
previous = current;
printf("you are already input %d string%s.\n",count, (count == 1) ? "" : "s");
printf("continue?(Y/N)");
fflush(stdin);
}
while(tolower((char)getchar()) == 'y');
//output strings and release memory
printf("output strings: \n");
current = first;
output_release(current, previous);
return 0;
}main function大家给点意见。。。。








