如何在C中的另一个struct数组中向数组中添加新元素?

时间:2016-01-04 19:37:24

标签: c arrays

现在我的代码看起来像这样,

    typedef struct veiculo
{
    char matricula[8];
    int nreixos;
    float alturaeixo;
    int tag;
    int classe;

}veiculo;

typedef struct portagem
{
    int nome;
    int kilometros;
    float preco;
    float total_recebido;
    struct veiculo carro[100];
}portagem;

(...)

int encontrar_ult_elemento(struct veiculo v[]){
    int d;
    int i;
    for(i=0;i<100;i++){
        if (v[i]!= 0)
        d++;
        else
            return d;
    }
}
void guardar_veiculo(struct portagem p[] ,struct veiculo v, int d){
    int i;
        if(v.tag=="1")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[1].carro[n]=v;
        else if(v.tag=="2")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[2].carro[n]=v;
        else if(v.tag=="3")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[3].carro[n]=v;
        else if(v.tag=="4")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[4].carro[n]=v;
        else if(v.tag=="5")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[5].carro[n]=v;
        else if(v.tag=="6")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[6].carro[n]=v;
        else if(v.tag="7")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[7].carro[n]=v;
        else if(v.tag="8")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[8].carro[n]=v;
        else if(v.tag="9")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[9].carro[n]=v;
        else if(v.tag="10")
            encontrar_ult_elemento(p[1].carro[])=n;
            p[10].carro[n]=v;
    }


    int main()
{
struct portagem portagem1;
portagem1.nome = 1;
portagem1.kilometros=14;
portagem1.preco=1.00;
portagem1.total_recebido;

struct portagem portagem2;
portagem2.nome = 2;
portagem2.kilometros=15;
portagem2.preco=1.05;
portagem2.total_recebido;

struct portagem portagem3;
portagem3.nome = 3;
portagem3.kilometros=7;
portagem3.preco=1.20;
portagem3.total_recebido;

(...)



struct portagem p[]={portagem1,portagem2,portagem3,portagem4,portagem5,portagem6,portagem7,portagem8,portagem9};

****我还有另外一个功能,要求用户注册veicule,分配相应的标签等......如果这是正确的方法,我就不会这样做。 谢谢你的帮助! ****

2 个答案:

答案 0 :(得分:2)

我认为你曾试图做过这样的事情:

void guardar_veiculo( portagem p[], veiculo v, int d ){
    int n;

    // if v.tag is greater or eaual 0 and less 9
    // then operate on p[v.tag].
    // I assume you meant 0..8 and not 1..10 ( struct portagem p[]= { }; in main )
    if ( v.tag >= 0 && v.tag < 9 )
    {
        // get next free element in p[v.tag].carro
        n = encontrar_ult_elemento( p[v.tag].carro );
        if ( n >= 0 && n < 100 )   // test if n is in range 0..99
            p[v.tag].carro[n] = v; // structure assignment
    }
}

您的函数encontrar_ult_elemento应始终返回值:

int encontrar_ult_elemento( veiculo v[] ){
    for( int d=0; d<100; d++ ){
        if ( v[d].tag == 0 ) // I assume your criteria is "tag", but I have to guess.
           return d;
    }
    return -1; // no empty element found
}

答案 1 :(得分:0)

您的代码最重要的问题是您正在将整数与可能编译但不正确的字符串文字进行比较。那不是你如何比较中的字符串,比较你需要一次一个字符所需的字符串,有一个库函数strcmp(3)来做这件事。

删除比较中数字的双引号,也许使用switch代替多个else if