将char数组的元素传递到另一个位置

时间:2018-12-27 23:02:28

标签: c string char

所以我有一个包含以下元素的结构:

typedef struct loja{
    int numero;
    char nome[MAX];
    float area;
    float faturacao[12];
} Loja[N];

我为该结构声明了一个数组vetC [],下一步是消除该数组的位置

int EliminarComum(int c,Loja vetC[]){    
    int posicao, i,a;  

    posicao = MostrarComum(c,vetC);  ///only for the user to choose the position he wishes to eliminate.
    if (posicao > c)  
        printf("can't delete.\n");  

    else {  
        for (i = posicao - 1; i < c - 1; i++){  
            vetC[i]->numero = vetC[i+1]->numero;  
            vetC[i]->nome = vetC[i+1]->nome;  
            vetC[i]->area = vetC[i+1]->area;  
            for(a=0;a<12;a++)  
                vetC[i]->faturacao[a] = vetC[i+1]->faturacao[a];  
            c--;  
        }  
    }  
    return c;  
}  

,在vetC[i]->nome = vetC[i+1]->nome;行中,出现错误

  

错误:分配给具有数组类型的表达式

1 个答案:

答案 0 :(得分:0)

您不能分配数组,但可以分配完整的struct loja对象:

vetC[i] = vetC[i+1];

例如,转换为以下简单程序,该程序说明了结构对象的分配是如何工作的,而字符数组的分配却失败了:

struct testStruct {

    int x;
    char str[10];
};

int main() {

    struct testStruct t1 = { 10, "Hello" };
    struct testStruct t2;

    t2 = t1;  // legal.

    char str1[10] = "Hello";
    char str2[10];

    // str2 = str1;  // illegal; arrays cannot be assigned.

    strcpy(str2,str1);  // legal (as long as str1 is a valid string)
}