如何使用函数将字符串添加到结构中?

问题描述 投票:0回答:1

我制作了一个停车系统,在该系统中,我使用了void功能输入了车辆的信息。但是我不知道如何通过使用void将字符串放入结构中。

这是我的代码。我的错误在哪里?

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


    struct car {
        char plate[10];
        char model[20];
        char color[10];
        };
    void main()
    {

        struct car c[4];

        AddCar(c[0],"43ds43","ford","blue");
        ShowCar(c[0]);

        return 0;
    }
        // I guess my mistake is here
        void AddCar (struct car c,char p[10],char m[10],char r[10]){
        strcpy(c.plate,p);
        strcpy(c.model,m);
        strcpy(c.color,r);
        }

    void ShowCar(struct car c){
        printf("Plate: %s   Model: %s  Color: %s\n-------",c.plate,c.model,c.color);}


c string struct char void
1个回答
0
投票

您正在复制struct car c。将其作为指针传递:

 AddCar(&c[0], "43ds43", "ford", "blue");
 // ...
 void AddCar(struct car *c,char p[10],char m[10],char r[10]) {
    strcpy(c->plate,p);
    strcpy(c->model,m);
    strcpy(c->color,r);
 }
© www.soinside.com 2019 - 2024. All rights reserved.