我在使用 get_string 函数时遇到问题

I am having issues with the get_string function

在 Windows Vscode 上经过大量试验后,我终于让 cs50 库正常工作了。现在,问题是 get_string 函数无法正常工作,如下所示:

int main(void)
{
    string s = get_string("Enter string: ");

    // ensure string was read
    if (s == NULL)
    {
        return 1;
    }

    string next = get_string("You just entered %s. Enter a new string: ", s);

    if (next == NULL)
    {
        return 1;
    }

    printf("Your last string was %s\n", s);
}

写的时候

string name = get_string("Give me a name:");

我收到错误

In file included from hello.c:1:0:
cs50.c:78:8: note: expected ‘char **’ but argument is of type ‘char *’
 string get_string(va_list *args, const string format, ...)
        ^~~~~~~~~~
hello.c:10:16: error: too few arguments to function ‘get_string’
  string name = get_string("Give me a name:");
                ^~~~~~~~~~
In file included from hello.c:1:0:
cs50.c:78:8: note: declared here
 string get_string(va_list *args, const string format, ...)

这是我的代码。我基本上是在测试函数中不需要的 get_string 函数。

#include "cs50.c"
#include "cs50.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{

 char str[20] = "#";
 string name = get_string("Give me a name:");
 printf("What height of pyramid \n");
 int user;
 if(scanf("%d", &user))
 {
     for (int i =0; i< 8; i++)
  {
      if(user <= 0 || user > 8 )
      {
          printf("Height: %d \n", user);
          printf("Provide value between 1 and 8 \n");
          scanf("%d", &user);

      }


  }
    printf("\n");
    int i;


    for (i = 1; i <= user; i++) { 
        for(int k = user; k > i; k--){
            putchar(' ');
        }
        int j;
        for (j = 0; j < i; j++) {

            putchar('#');

        }
        putchar('\n');

    }
  return EXIT_FAILURE;
 }

}

我希望写

string s = get_string("Enter string: ");

并在 运行 代码时在终端中得到提示。

事实上,通过反复试验并没有给您带来完全正确的解决方案。 问题比较简单。您应该使用的 get_string 是来自 cs50.h 的宏。 cs50.c 删除了 这个宏定义并定义了另一个名为 get_string 的函数(是的,这很糟糕)。最终结果是您无法 #include <cs50.c> 使代码即使在单文件应用程序中也能正常工作。

你只需要

#include <cs50.h>

并添加 cs50.c 作为项目中的 另一个 翻译单元,即如果您的主程序是 prog.c 您将添加 cs50.c 作为同一文件夹中的一个文件。

我能够通过包含来自 libcs​​50-8.0.3 的 cs50.h 和 cs50.c 来解决这个问题,这是符合我想要的 v8.0.3。

现在一切都很好。

如果你添加了cs50库,甚至出现这个错误,我可以给出这样的建议:

对于linux终端;

clang -o file_name file_name.c -lcs50

来源:哈佛大​​学 CS50

将此代码复制并粘贴到您的源代码文件中:

我自己的 CS50 Get_String 函数(简化实现)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define SIZE_MAX 50

char *get_string(const char *format, ...)
{
    if (format != NULL)
    {
        va_list ap;

        va_start(ap, format);

        vprintf(format, ap);

        va_end(ap);
    }

    char *buffer = NULL;
    size_t capacity = 0;
    size_t size = 0;
    unsigned read = 0;
    int c;

    while ((c = fgetc(stdin)) != '\r' && c != '\n' && c != EOF)
    { 
        read++;

        if (read > capacity && read < SIZE_MAX )
            capacity++;
            
        else
        {
            free(buffer);
            return NULL;
        }

        char *temp = realloc(buffer, capacity);
        if (temp == NULL)
        {
            free(buffer);   
            return NULL;
        }
        buffer = temp;
        buffer[size++] = c;
    }
    
    if (size == 0 || c == '\n')
        return NULL;

    if (size == SIZE_MAX)
    {
        free(buffer);
        return NULL;
    }

    char *s = realloc(buffer, size + sizeof(char));
    if (s == NULL)
    {
        free(buffer);
        return NULL;
    }
    s[size] = '[=10=]';

    return s;
}

我找到了解决这个问题的方法!在 'get_string 中调用字符串 's' 变量' 函数解决了这个问题。

#include <cs50.c>
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    printf("Enter a string: ");
    string s = get_string(" ", s);
    printf("%s", s);

注: 我不知道这种方法是否会导致错误。它对我来说很好用。