繁体   English   中英

为什么我的 C 程序没有打印出字符串?

[英]Why is my C program not printing out the string?

我正在制作一个程序来打印出用户输入的内容,并且需要使用read_line()方法(对于我的作业)来完成,所以我不能改变太多。

我不明白为什么它没有打印出用户输入的内容。

#include <stdlib.h>

char *read_line(char *buf, size_t sz) {
  char tempBuf[sz];
  char c;
  int pos = 0;

  printf("> ");

  while(1) {
    c = getchar();
    if (tempBuf[pos] == EOF || tempBuf[pos] == '\n') {
      buf = tempBuf;
      return buf;
    } else {
      tempBuf[pos] = c;
    }
    pos++;
  }
}

int main(int argc, char **argv) {
  char *buf;
  char *input = read_line(buf, 128);

  printf("Here: %s", input);
}

我是 C 的新手,我发现它非常混乱,所以请用非常简单的术语解释任何事情。 任何帮助将非常感激。

char *buf;
char *input = read_line(buf, 128);

您的第一行创建了一个名为buf的指针变量,但没有为其分配任何特定值。 您的第二行将buf的值传递给read_line - 但您从未为其分配任何特定值。 因此,您将垃圾传递给read_line并告诉它使用该垃圾作为缓冲区。

你可能想要char buf[128]; 而不是char *buf; ,但很难说。

另外,请参阅我关于您的read_line function 被破坏的评论,这表明编写它的人不了解如何使用getchar

你的程序有很多错误。

对于初学者,您将一个未初始化的指针传递给 function

char *buf;
char *input = read_line(buf, 128);

实际上,传递 function 中未使用的指针值是没有意义的。

在 function 中,变量 c 应声明为具有 int 类型。

int c;

否则,如果 char 类型表现为 unsigned char 类型(它取决于编译器选项),则它与EOF的比较将始终评估为 false。

在 function 中,数组 tempBuf 未初始化。 所以这个 if 语句

if (tempBuf[pos] == EOF || tempBuf[pos] == '\n') {

调用未定义的行为。

在 wjile llop 中,您必须检查pos的值是否小于sz的值。

function 返回一个指向本地数组的指针,使返回的指针无效。

  buf = tempBuf;
  return buf;

此外,该数组甚至不包含字符串,因为终止零未附加到数组中。

function 应动态分配 memory 并返回指向已分配 memory 的指针,该指针将包含一个以零结尾的字符串。

下面是一个演示程序,显示了如何编写 function。

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

char * read_line( size_t n ) 
{
    char *s = malloc( n );

    if ( s != NULL )
    {
        int c;

        printf( "> " );

        size_t i = 0;

        for ( ; i + 1 < n && ( c = getchar() ) != EOF && c != '\n'; i++ )
        {
            s[i] = c;
        }

        s[i] = '\0';
    }

    return s;
}

int main(void) 
{
    size_t n = 128;

    char *input = read_line( n );

    if ( input != NULL ) printf( "Here: %s\n", input );

    free( input );

    return 0;
}

程序 output 可能看起来像

> Hello Jake Jackson
Here: Hello Jake Jackson

read_line的当前实现存在很大缺陷,因为它返回一个本地声明的缓冲区(在函数末尾被删除)。 结果,您的指针指向垃圾值(这是非常危险的 - 我重复一遍 - 因为它会导致您的程序崩溃或更糟[即使使用它不拥有的 memory 也继续运行])。

应该做的是在堆上动态创建缓冲区(这样可以安全地返回它 - 但必须手动删除它)。

因此,您的 function (以及代码的 rest )应更改为如下所示:

#include <stdlib.h>

char *read_line(size_t sz) {
  // create a new buffer on the heap - so it can easily be returned (note this will have to be deleted after use using free()).
  char *tempBuf = malloc(sz);
  int c;
  int pos = 0;

  // set all elements of tempBuf to nulls
  memset(arr, 0, sz); 

  printf("> ");

  while(1) {
    c = getchar();
    if (tempBuf[pos] == EOF || tempBuf[pos] == '\n') {
      return tempBuf;
    } else {
      tempBuf[pos] = (char) c;
    }
    pos++;
  }
}

int main(int argc, char **argv) {
  char *input = read_line(128);    
  printf("Here: %s", input);

  // free the memory since we are now done with it.
  free(input);
}

您可以在此处阅读有关堆和堆栈的更多信息。

您的程序存在一些问题。

在您的main()中,您初始化buf ,但从不为其分配任何 memory 。 您可以使用malloc

    char *buf = malloc(128);
    char *input = read_line(buf, 128);

read_line()中,您初始化tempBuf ,但在初始化元素之前从中读取一个元素。 tempBuf包含用于比较的垃圾值。 您在比较初始化元素,这是不正确的。 还有其他问题。

这是一个修改后的版本,对您的代码进行了最小的更改:

char *read_line(char *buf, size_t sz) {
  char *tempBuf = malloc(sz);
  char c;
  int pos = 0;

  printf("> ");

  while(1) {
    c = getchar();
    if (c == '\n') {
      tempBuf[pos] = '\0';
      buf = tempBuf;
      return buf;
    } else {
      tempBuf[pos] = c;
    }
    pos++;
  }
}

C 中任何字符串的最后一个字符必须是 null 字符('\0')。

提示:您还应该添加对pos的检查,因此它不会超过sz的值。

试试这个:

#include<stdio.h>       //originally missing - needed for I/O
#include<string.h>      //for memset

void read_line(char *buf, size_t sz) 
{
   char c;
   int pos = 0;

   printf("> ");

   while(pos<sz-1) 
   {
       c = getchar();
       if (c == EOF || c == '\n')  //originally tempBuf[pos] was used which contained garbage value
       {
           break;
       } 
       else 
       {
           buf[pos] = c;
       }
       pos++;
   }
}

 int main(int argc, char **argv) 
 {
    char buf[128];             //buf is now an array instead of char*
    memset(buf,'\0',128);
    read_line(buf, 128);    //buf is the array in this code as the array tempBuf is local and will be destroyed once we come out of read_line function 

    printf("Here: %s", buf);
 }

此代码中已对错误进行了注释。

你有很多错误。 在 scope 之外使用的局部变量,未终止的字符串等。这里有工作示例https://godbolt.org/z/_3ZHUJ

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

char *read_line(size_t sz) 
{
  char *tempBuf = malloc(sz);
  char c;
  int pos = 0;

  printf("> ");

  while(1) {
    c = getchar();
      tempBuf[pos] = c;
    if (tempBuf[pos] == EOF || tempBuf[pos] == '\n') 
    {
      tempBuf[pos] = 0;
      return tempBuf;
    }
    pos++;
  }
}

int main(int argc, char **argv) {
  char *buf;
  char *input = read_line( 128);

  printf("Here: %s", input);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM