c - 将带有 typedef 的结构数组传递给函数

标签 c arrays string struct

我需要 C 编程方面的帮助。我有以下情况:

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

typedef struct Product products[8];
products product = {
    {100, "Mouse", "Ottico", 10, 8.30},
    {101, "Tastiera", "Wireless", 6, 15.50},
    {102, "Monitor", "LCD", 3, 150.25},
    {103, "Webcam", "USB", 12, 12.00},
    {104, "Stampante", "A Inchiostro", 6, 100.00},
    {105, "Scanner", "Alta Risoluzione", 9, 70.50},
    {106, "Router", "300 Mbps", 10, 80.30},
    {107, "Lettore Mp3", "10 GB", 16, 100.00}
    };

请忽略上面使用的意大利语。

我想将名为“product”的结构数组传递给一个函数。例如,如果我想做类似的事情

product[1].name = "Computer"

但是在函数内部,我应该怎么做呢?我想知道如何从 main() 调用该函数以及如何在我的头文件中编写原型(prototype)。

在此先感谢您的帮助。

编辑

我给你这个测试程序。这个不工作,甚至没有调用 main 中的函数。它只是无法编译。

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

void test(Card *card);

int main()
{
    struct Card {
        char *type;
        char *name;
    };

    typedef struct Card cards[2];
    cards card = {{"Hi", "Hi"}, {"Foo", "Foo"}};
    return 0;
}

void test(Card *card) {
    printf("%s", card[1].type);
}

最佳答案

这里:

void foo(struct Product *bla)
{
    bla[1].name = "Computer";
}

或者使用你的类型别名

void foo(products bla)
{
    bla[1].name = "Computer";
}

然后像这样调用函数:

foo(product); 

关于c - 将带有 typedef 的结构数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9084575/

相关文章:

c++ - 将 Int 指针转换/类型转换为 char 指针,但获取 ascii 字符?

javascript - 如何展示具体的JSON数据?

java - 无法发现程序中的空异常

javascript - 类型 number[][] 不可分配给类型 number[]

python - 我收到类型错误 : '<' not supported between instances of 'str' and 'int'

c - 如何将阿拉伯语 ascii 文本转换为十六进制?

c - "open_file:"在c中的含义?

c - printf 调试以跟踪函数

python - 正则表达式组恰好匹配 n 次

c++ - 用户输入字符串的耦合算法