将带有typedef的struct数组传递给函数

时间:2021-10-14 21:31:59

I need help with C programming. I have the following situation:

我需要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}
    };

Please disregard the use of Italian language above.

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

I would like to pass the array of structs named "product" to a function. For example, if I wanted to do something like

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

product[1].name = "Computer"

But inside of a function, how am I supposed to do it? I would like to know how to call that function from the main() and how to write the prototype in my header file.

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

Thanks in advance for any help.

在此先感谢您的帮助。

EDIT

I'm giving you this test program. This one ain't working and there is not even call to function in the main. It just doesn't compile.

我给你这个测试程序。这个不起作用,甚至没有主要功能调用。它只是不编译。

#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);
}

3 个解决方案

#1


6  

Here:

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

or using your type alias

或使用您的类型别名

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

then call the function like this:

然后像这样调用函数:

foo(product); 

#2


2  

Since you have the typedef (which is missing a struct keyword in your example, by the way), you can just use that type in the function prototype:

由于你有typedef(在你的例子中缺少一个struct关键字,顺便说一句),你可以在函数原型中使用该类型:

void func(products p);

A function performing the specific operation you asked about might be:

执行您询问的特定操作的函数可能是:

void func(products p)
{
  p[1].name = "Computer";
}

You can call it like:

你可以这样称呼:

func(product);

From anywhere where product is in scope.

从产品在范围内的任何地方。

#3


0  

typedef struct tag_Product {
    int code;
    char *name;
    char *spec;
    int quantity;
    float price;
} PRODUCT;

PRODUCT products[8] = {
    {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}
    };

void MyFunction(PRODUCT *pProduct)
{
   pProduct->code = 0; // sample
}

void main()
{
   MyFunction(&products[2]); // sample
}

#1


6  

Here:

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

or using your type alias

或使用您的类型别名

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

then call the function like this:

然后像这样调用函数:

foo(product); 

#2


2  

Since you have the typedef (which is missing a struct keyword in your example, by the way), you can just use that type in the function prototype:

由于你有typedef(在你的例子中缺少一个struct关键字,顺便说一句),你可以在函数原型中使用该类型:

void func(products p);

A function performing the specific operation you asked about might be:

执行您询问的特定操作的函数可能是:

void func(products p)
{
  p[1].name = "Computer";
}

You can call it like:

你可以这样称呼:

func(product);

From anywhere where product is in scope.

从产品在范围内的任何地方。

#3


0  

typedef struct tag_Product {
    int code;
    char *name;
    char *spec;
    int quantity;
    float price;
} PRODUCT;

PRODUCT products[8] = {
    {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}
    };

void MyFunction(PRODUCT *pProduct)
{
   pProduct->code = 0; // sample
}

void main()
{
   MyFunction(&products[2]); // sample
}