C语言 我遇到了get_string函数的问题

rvpgvaaj  于 2023-06-05  发布在  其他
关注(0)|答案(7)|浏览(156)

我刚刚终于得到了cs50库工作后,我的Windows Vscode大量的试验。现在,问题是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: ");

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

ig9co6j1

ig9co6j11#

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

#include <cs50.h>

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

ruarlubt

ruarlubt2#

我能够通过包含libcs 50 -8.0.3中的cs50.h和cs50.c来解决这个问题,libcs 50 -8.0.3是符合我想要的v8.0.3。
现在一切都好了。

bqucvtff

bqucvtff3#

如果你添加了cs50库,甚至你得到了这个错误,我可以给予这样的建议:
用于linux终端;

clang -o file_name file_name.c -lcs50

来源:哈佛College CS50

flmtquvp

flmtquvp4#

将以下代码复制并粘贴到源代码文件中:
我自己的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] = '\0';

    return s;
}
kt06eoxx

kt06eoxx5#

这对我很有效:

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

int main(void)
{
    char c1 = ' ', *c2, **c3;
    c2 = &c1;
    c3 = &c2;
    string s = get_string(c3, "Some text: ");
    printf("%s\n", s);
}
jjjwad0x

jjjwad0x6#

我注意到get_string检查cs50.c文件中的NULL,因此将第一个参数设置为NULL为我修复了它。

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

uinbv5nw7#

我有一个解决这个问题的方法!在'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);

**注意:**我不知道这种方法是否会导致bug。我觉得挺好的。

相关问题