为什么从此结构打印会出现分段错误?

发布于 2025-01-14 13:13:55 字数 1549 浏览 2 评论 0原文

我试图创建一个 Product 结构数组,然后打印数组中每个 Product 的名称和代码,但我不断收到分段错误。我尝试在没有循环的情况下插入每个值,然后打印,它可以工作,但我想自动化它。函数 fill_products 根据用户的输入填充产品数组,而 select_products 则打印整个数组的每个名称-代码对。

这是我的代码:

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

typedef struct
{
    int code;
    char *name;
    float price;
} Product;

void select_products(Product *products, int len)
{
    int i;

    printf("%-30s%s\n", "Name", "Code");
    for (i = 0; i < len; i++)
    {
        printf("%-30s%d\n", products[i].name, products[i].code);
    }

    return;
}

void fill_products(Product *products, int len)
{
    int i, code;
    char *name;
    float price;

    for (i = 0; i < len; i++)
    {
        printf("Insert product name (%d / %d): ", i + 1, len);
        scanf("%s", &name);
        printf("Insert product price (%d / %d): ", i + 1, len);
        scanf("%f", &price);

        products[i].code = i;
        products[i].name = name;
        products[i].price = price;
    }

    return;
}

int is_alloc(Product *products)
{
    if (products == NULL)
    {
        printf("Error: memory allocation unsuccessful.\n");
    }
    return products != NULL;
}

int main(void)
{
    int len, n_bytes;
    Product *products;

    printf("Insert length of array: ");
    scanf("%d", &len);

    n_bytes = sizeof *products * len;
    products = malloc(n_bytes);

    if(!is_alloc(products))
    {
        exit(0);
    }

    fill_products(products, len);
    select_products(products, len);

    free(products);

    return 0;
}

I'm trying to create an array of Product structs and then print the name and code of each Product in the array, but I keep getting a segmentation fault. I have tried to insert each value without a loop and then printing, and it works, but I'd like to automate it. The function fill_products fills the products array according to the user's input, and the select_products prints each name-code pair for the entire array.

This is my code:

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

typedef struct
{
    int code;
    char *name;
    float price;
} Product;

void select_products(Product *products, int len)
{
    int i;

    printf("%-30s%s\n", "Name", "Code");
    for (i = 0; i < len; i++)
    {
        printf("%-30s%d\n", products[i].name, products[i].code);
    }

    return;
}

void fill_products(Product *products, int len)
{
    int i, code;
    char *name;
    float price;

    for (i = 0; i < len; i++)
    {
        printf("Insert product name (%d / %d): ", i + 1, len);
        scanf("%s", &name);
        printf("Insert product price (%d / %d): ", i + 1, len);
        scanf("%f", &price);

        products[i].code = i;
        products[i].name = name;
        products[i].price = price;
    }

    return;
}

int is_alloc(Product *products)
{
    if (products == NULL)
    {
        printf("Error: memory allocation unsuccessful.\n");
    }
    return products != NULL;
}

int main(void)
{
    int len, n_bytes;
    Product *products;

    printf("Insert length of array: ");
    scanf("%d", &len);

    n_bytes = sizeof *products * len;
    products = malloc(n_bytes);

    if(!is_alloc(products))
    {
        exit(0);
    }

    fill_products(products, len);
    select_products(products, len);

    free(products);

    return 0;
}

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

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

发布评论

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

评论(1

以为你会在 2025-01-21 13:13:55

我不断遇到分段错误。

请启用编译器警告,并注意它们。

这段代码

    char *name;
...
        scanf("%s", &name);

是假的,根本不符合您的预期。

您必须单独为name分配空间(然后不要忘记free()它),或者使该空间在name中可用。 >Product 结构如下:(

typedef struct
{
    int code;
    char name[100];
    float price;
} Product;

假设 name 长度有合理的限制)。

I keep getting a segmentation fault.

Please enable compiler warnings, and pay attention to them.

This code:

    char *name;
...
        scanf("%s", &name);

is bogus and doesn't do at all what you intend.

You must either allocate space for name separately (and then not forget to free() it), or make that space available in the Product structure like so:

typedef struct
{
    int code;
    char name[100];
    float price;
} Product;

(this assumes there is a reasonable limit on name length).

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