Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
strcpy不会将源字符串复制到dest_C - Fatal编程技术网

strcpy不会将源字符串复制到dest

strcpy不会将源字符串复制到dest,c,C,strcpy不适合我 当我执行下面的代码时,它会在我用EOF输入后在屏幕上打印注释 我试着用gdb调试它,发现line没有复制到p 我是一个新的鱼与c,我在这个问题上结巴。请帮帮我 代码: #包括 #包括 int getline1(char*,int); 字符*alloc(int); 空无(字符*); 内部主(空) { 字符*p,第[1000]行; 内伦; len=getline1(第1000行); p=alloc(len); 行[len-1]='\0'; strcpy(p,line); pri

strcpy不适合我

当我执行下面的代码时,它会在我用
EOF
输入后在屏幕上打印注释

我试着用
gdb
调试它,发现
line
没有复制到
p

我是一个新的鱼与c,我在这个问题上结巴。请帮帮我

代码:

#包括
#包括
int getline1(char*,int);
字符*alloc(int);
空无(字符*);
内部主(空)
{
字符*p,第[1000]行;
内伦;
len=getline1(第1000行);
p=alloc(len);
行[len-1]='\0';
strcpy(p,line);
printf(“%s\n”,p);
返回0;
}
/*getline:将行放入s,返回长度*/
int getline1(字符*s,int lim)
{
int c,i;
i=0;
而(--lim>0&&(c=getchar())!=EOF&&c!='\n')
s[i++]=c;
如果(c=='\n')
s[i++]=c;
*s='\0';
返回i;
}
#定义ALLOCSIZE 10000/*可用空间大小*/
静态字符allocbuf[ALLOCSIZE];/*alloc存储*/
静态字符*allocp=allocbuf;/*下一个自由位置*/
char*alloc(int n)/*返回指向n个字符的指针*/
{
如果(allocbuf+ALLOCSIZE-allocp>=n){
/*适合*/
allocp+=n;
返回allocp-n;/*旧p*/
}否则{/*没有足够的空间*/
返回0;
}
}
免费(char*p)/*免费存储*/
{
如果(p>=allocbuf&&p
getline1()函数中出现了错误:在返回语句之前有“*s=”\0“;”,它实际上将s[0]设置为NUL。因此,您总是返回一个空字符串,因为第一个字节被设置为“\0”。

getline1()函数出错:在返回语句之前,您有“*s=”\0“;”,它实际上将s[0]设置为NUL。因此,您总是返回一个空字符串,因为第一个字节设置为“\0”。

请显示输入和预期输出与实际输出的对比示例。您是否检查了
getline1
是否正常工作?更改
*s='\0'
s[i]='\0'@shanechiu结论。编写一个函数,测试它,然后在它工作时继续。学习如何使用调试器。请演示输入和预期输出与实际输出的对比示例。是否检查了
getline1
是否正常工作?更改
*s='\0'
s[i]='\0'@shanechiu结论。编写一个函数,测试它,然后在它工作时继续。学习如何使用调试器。
#include <stdio.h>
#include <string.h>

int getline1(char *, int);

char *alloc(int);

void afree(char *);

int main(void)
{
    char *p, line[1000];
    int len;

    len = getline1(line, 1000);

    p = alloc(len);

    line[len - 1] = '\0';
    strcpy(p, line);
    printf("%s\n", p);
    return 0;
}

/* getline: get line into s, return length */
int getline1 (char *s, int lim)
{
        int c, i;

        i = 0;
        while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
                s[i++] = c;
        if (c == '\n')
                s[i++] = c;
        *s = '\0';
        return i;
}

#define ALLOCSIZE   10000   /* size of available space */

static char allocbuf[ALLOCSIZE];    /* storage for alloc */
static char *allocp = allocbuf; /* next free position */

char *alloc(int n)      /* return pointer to n characters */
{
    if (allocbuf + ALLOCSIZE - allocp >= n) {
        /* it fits */
        allocp += n;
        return allocp - n;  /* old p */ 
    } else {    /* not enough room */
        return 0;
    }
}

void afree(char *p) /* free storage pinted to by p */
{
    if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
        allocp = p;
}