相关疑难解决方法(0)

realloc()无效的旧大小

我正在从KandR C编程书中做一些有趣的练习.该程序用于从用户输入的一组行中查找最长行,然后打印它.

这是我写的(部分,部分内容直接取自本书): -

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

int MAXLINE =  10;
int INCREMENT = 10;

void copy(char longest[], char line[]){
    int i=0;

    while((longest[i] = line[i]) != '\0'){
        ++i;
    }
}

int _getline(char s[]){
    int i,c;

    for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){
        if(i == MAXLINE - 1){
            s = (char*)realloc(s,MAXLINE + INCREMENT);

            if(s == NULL){
                printf("%s","Unable to allocate memory");
                //  goto ADDNULL;
                exit(1);
            }

            MAXLINE = MAXLINE + INCREMENT;
        }
        s[i] = c;
    }

    if(c == '\n'){
        s[i] = c;
        ++i; …
Run Code Online (Sandbox Code Playgroud)

c realloc dynamic-memory-allocation

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×1

dynamic-memory-allocation ×1

realloc ×1