realloc.c

来自「c21Examples.rar」· C语言 代码 · 共 40 行

C
40
字号
/* Using realloc() to change memory allocation. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void )
{
    char buf[80], *message;

    /* Input a string. */

    puts("Enter a line of text.");
    gets(buf);

    /* Allocate the initial block and copy the string to it. */

    message = realloc(NULL, strlen(buf)+1);
    strcpy(message, buf);

    /* Display the message. */

    puts(message);

    /* Get another string from the user. */

    puts("Enter another line of text.");
    gets(buf);

    /* Increase the allocation, then concatenate the string to it. */

    message = realloc(message,(strlen(message) + strlen(buf)+1));
    strcat(message, buf);

    /* Display the new message. */
    puts(message);
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?