| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 425 人关注过本帖
标题:让代码飞_动态内存分配函数解决动态数组问题_第四版
只看楼主 加入收藏
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:2 
让代码飞_动态内存分配函数解决动态数组问题_第四版
我写的是一个,不限制长度和个数的字符串输入/输出函数。动态数组一直是一个头疼的问题,相信大家也遇到过。大家可以拿过去测试一下。
程序代码:
 /***************************************************************************

 *                                                                          *

 * 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
大家给点意见。。。。
搜索更多相关主题的帖子: 字符串 动态 
2015-06-09 21:21
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
收藏
得分:0 
发现论坛一个BUG,发表一个帖子不能同时粘贴多个程序,一旦粘贴第二个,第二个程序就会覆盖到第一个粘贴的程序里面去!!!
程序代码:
#ifndef _STDC_WANT_LIB_EXT1_
#define _STDC_WANT_LIB_EXT1_  1
#endif
#define buffer_len 10
#define buffer_len_inc 5
typedef struct string string;
struct string
{
    char* start;
    char* end;
    int str_length;
    string* next;
};

 //function prototype
 string* sturct_memory(size_t size);

 char* memory_init(size_t size);

 string* get_string(int length, string* buf);

 string* store_strings(string* buf);

 string* add_buf_memory(int length, string* buff);

 void output_release(string* cur, string* pre);
为了让主函数短一点,自己搞得头文件"unli_str.h"和c文件。
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "unli_str.h"
/***************function declare*****************/

 string* sturct_memory(size_t size)

 {
     string* temp = NULL;
     temp = (string*)malloc(size);
     if(temp == NULL)
     {
         fprintf_s(stderr, "malloc() function error!!!\n");
         exit(1);
     }
     return temp;

 }

 //memory initialization for input buffer
 char* memory_init(size_t size)

 {
     char* p_buf = NULL;
     p_buf = (char*)malloc(size);
        if(p_buf == NULL)
        {
            fprintf_s(stderr, "malloc() function error!!!\n");
            exit(1);
        }
     return p_buf;

 }

 //input strings
 string* get_string(int length, string* buf)

 {
     int buff_len = length;
     fflush(stdin);
     do
     {
         *(buf->end) = getchar();
         (buf->end)++;
         if(buf->end - buf->start == length)     
        buf = add_buf_memory(buff_len, buf);    
     }
     while(*(buf->end - 1) != '\n');
     return buf;

 }

 //store strings
string* store_strings(string* buf)

 {
     char* store_temp = NULL;
     int test_int = 0;
     store_temp = (char*)malloc(buf->str_length);
     if(store_temp == NULL)
     {
         fprintf_s(stderr, "malloc() function error!!!\n");
         exit(1);
     }
     test_int = strcpy_s(store_temp, buf->str_length + 1, buf->start);
     if(test_int == !0)
     {
         fprintf_s(stderr, "strcpy_s() function error!!!\n");
         exit(1);
     }
     buf->start = store_temp;
     return buf;

 }

 //output and release function
 void output_release(string* cur, string* pre)

 {

 do
     {
         printf_s("%s\n", cur->start);
         pre = cur;
         cur = cur->next;
         free(pre);
         pre = NULL;
     }
     while(cur != NULL);

 }

 //add buffer memory
 string* add_buf_memory(int length, string* buff)

 {
        char* buf_temp = NULL;
         length += buffer_len_inc;
         buf_temp = (char*)realloc(buff->start, length * sizeof(char));
         if(buf_temp == NULL)
         {
             fprintf_s(stderr, "realloc() function error!!!\n");
             exit(1);
         }
         buff->end = buf_temp + (buff->end - buff->start);
         buff->start = buf_temp;
         buf_temp = NULL;
         return buff;  
}
我在这怎么又可以粘贴两个程序???奇怪
2015-06-09 21:26
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:14 
测试一下
2015-06-09 21:41
快速回复:让代码飞_动态内存分配函数解决动态数组问题_第四版
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017324 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved