使用 scanf 动态数据结构

发布于 2024-10-16 07:00:40 字数 1237 浏览 6 评论 0原文

我有一个非常基本的问题,感谢您的耐心等待。

我有一个动态数据结构,只有一个整数值和一个指向下一个结构的指针。我使用 scanf 获取用户输入以获取 5 个值以添加到结构中,并尝试在最后打印输出。我在将输入输入到结构中时遇到语法问题。我已经浏览了 StackOverflow 和 Google,但没有效果(可能是因为它太基础了!)

这里是代码:

#include <stdio.h>

struct List
{
    int value;
    struct List *nextaddr;
};

int main()
{
    int int1, int2, int3, int4, int5;

    printf("please enter the first integer: ");
    scanf("%d", int1);
    struct List t1 = {int1};

    printf("please enter the second integer: ");
    scanf("%d", int2);
    struct List t2 = {int2};

    printf("please enter the third integer: ");
    scanf("%d", int3);
    struct List t3 = {int3};

    printf("please enter the fourth integer: ");
    scanf("%d", int4);
    struct List t4 = {int4};

    printf("please enter the fifth integer: ");
    scanf("%d", int5);
    struct List t5 = {int5};

    struct List *first;

    first = &t1;
    t1.nextaddr = &t2;
    t2.nextaddr = &t3;
    t3.nextaddr = &t4;
    t4.nextaddr = &t5;
    t5.nextaddr = NULL;

    printf("%i\n%i\n%i\n%i\n%i\n",first->value,t1.nextaddr->value,t2.nextaddr->value,t3.nextaddr->value,t4.nextaddr->value);

    return 0;
}

如何将用户输入获取到结构中?

I have a very basic question, thanks for your patience.

I have a dynamic data structure with just an integer value and a pointer to the next structure. I am using scanf to get the user input to get 5 values to add to the structure and trying to print the output at the end. I am having trouble with the syntax to get the input into the structure. I have looked around StackOverflow and Google, with no avail (probably because it is too basic!)

here is the code:

#include <stdio.h>

struct List
{
    int value;
    struct List *nextaddr;
};

int main()
{
    int int1, int2, int3, int4, int5;

    printf("please enter the first integer: ");
    scanf("%d", int1);
    struct List t1 = {int1};

    printf("please enter the second integer: ");
    scanf("%d", int2);
    struct List t2 = {int2};

    printf("please enter the third integer: ");
    scanf("%d", int3);
    struct List t3 = {int3};

    printf("please enter the fourth integer: ");
    scanf("%d", int4);
    struct List t4 = {int4};

    printf("please enter the fifth integer: ");
    scanf("%d", int5);
    struct List t5 = {int5};

    struct List *first;

    first = &t1;
    t1.nextaddr = &t2;
    t2.nextaddr = &t3;
    t3.nextaddr = &t4;
    t4.nextaddr = &t5;
    t5.nextaddr = NULL;

    printf("%i\n%i\n%i\n%i\n%i\n",first->value,t1.nextaddr->value,t2.nextaddr->value,t3.nextaddr->value,t4.nextaddr->value);

    return 0;
}

How can I get the user input into the structure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤慢歌 2024-10-23 07:00:40

有几点 -

通常,当使用链接列表等链接结构时(正如本例所示),您可以使用 malloc 在堆上而不是在堆栈上为对象分配内存。其背后的基本原理是,您希望链接结构的元素比创建它们的函数寿命更长,因此堆栈分配各个单元不太可能正常工作。 来创建结构

struct List* entry = malloc(sizeof(struct List));

您可能希望通过编写类似作为后续内容的内容 ,因为从用户读取链接列表单元格的内容并将其添加到列表中的逻辑对于您正在读取的每个单元格是相同的,您可能不想只复制它的代码五次。相反,请考虑编写这样的函数:

struct List* ReadListEntry(void) {
    struct List* entry = malloc(sizeof(struct List));
    /* ... initialize 'entry' ... */

    return entry;
}

这​​样,您在 main 中的代码可以缩短五倍,并且如果您在代码中发现任何错误(正如您似乎所做的那样),您只需要改变一次,而不是五次。

至于您原来的问题,我认为问题是您正在编写

scanf("%d", myValue);

而不是

scanf("%d", &myValue);

第一个版本不正确,并且可能会导致运行时崩溃。 scanf 假定您在使用 %d 格式说明符时提供指向整数的指针作为参数,而不是整数,所以明确的&符号可能是伤害你的原因。

将其与上述使用辅助函数生成堆分配列表单元的想法相结合,您可能想尝试编写这样的函数:

struct List* ReadListEntry(void) {
    struct List* entry = malloc(sizeof(struct List));

    scanf("%d", &entry->value);
    entry->nextaddr = NULL;

    return entry;
}

鉴于此,您可能可以将您的 main 函数重写为更多比你现在拥有的更简单。我将把它作为练习留给读者。 :-)

希望这有帮助!

A few things-

Typically, when working with a linked structure like a linked list (as you are in this case), you allocate the memory for the objects on the heap using malloc rather than on the stack. The rationale behind this is that you want the elements of a linked structure to outlive the function that creates them, and so stack-allocating the individual cells isn't likely to work correctly. You probably want to create the structure by writing something like

struct List* entry = malloc(sizeof(struct List));

As a follow-up, since the logic for reading the contents of a linked list cell from the user and adding it to the list is identical for each cell you're reading, you probably don't want to just copy the code for it five times. Instead, consider writing a function like this:

struct List* ReadListEntry(void) {
    struct List* entry = malloc(sizeof(struct List));
    /* ... initialize 'entry' ... */

    return entry;
}

That way, your code in main can be five times shorter, and if you find any bugs in the code (as you seem to have done), you only need to change it once, not five times.

As for your original question, I think that the problem is that you're writing

scanf("%d", myValue);

instead of

scanf("%d", &myValue);

This first version is incorrect and likely to cause a crash at runtime. scanf assumes that you're providing a pointer to an integer, not an integer, as the argument whenever you use the %d format specifier, so the explicit ampersand is probably what's hurting you.

Combining this with the above idea to use a helper function to produce heap-allocated list cells, you might want to try writing a function like this:

struct List* ReadListEntry(void) {
    struct List* entry = malloc(sizeof(struct List));

    scanf("%d", &entry->value);
    entry->nextaddr = NULL;

    return entry;
}

Given this, you can probably rewrite your main function to be much simpler than what you have now. I'll leave that as an exercise to the reader. :-)

Hope this helps!

如何视而不见 2024-10-23 07:00:40

scanf 应该获取整数的地址,如下所示:scanf("%d", &int1);

scanf should get the address of the integer as in: scanf("%d", &int1);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文