Why do I get this error "assignment too expression with array type" is a doubly linked list

1
struct Fecha{
  int dia;
  int mes;
  int anio;
};

struct nodoeventos{
  struct nodoeventos *Antptr;
  char ID[5];
  char Nombre[20];
  char Desc[40];
  struct Fecha fecha;
  struct nodoeventos *Sigptr;
};

typedef struct nodoeventos NodoE;

/**FUNCION CREAR NODO**/
void crear(NodoE **inicioptr,char Id)
{
    NodoE *NuevoPtr;
    NuevoPtr=malloc(sizeof(NodoE));
    if(NuevoPtr!=NULL)
    {
      (NuevoPtr)->Antptr=NULL;
      (NuevoPtr)->ID=Id;
      (NuevoPtr)->Nombre=nombre;
      (NuevoPtr)->Desc=desc;
      (NuevoPtr)->fecha->dia=Dia;
      (NuevoPtr)->fecha->mes=Mes;
      (NuevoPtr)->fecha->anio=Anio;
      (NuevoPtr)->Sigptr=NULL;
    }
    else
    {
       printf("Memoria llena");
    }
}
    
asked by Diana Lizeth 15.07.2017 в 22:02
source

1 answer

2

You are defining the struct ID member as

char ID[5];

When you use NuevoPtr->ID (without subscripts) in an expression, what you really get is the memory address of the beginning of the array . Since the array memory is in a fixed position, you simply can not assign it a new value.

If you want to change values, you will have to do so by specifying the indexes

NewPtr-> ID [0] = id;

If you had defined ID as a pointer ( char *ID ), you could assign it the memory address that pointer would point to.

Additionally, you are trying to assign a char to what is an array of 5 positions. Dont have much sense; normally what you would expect is that the value you assign is compatible with the variable you assign to (although, as I explained earlier, in the case of arrays, even if they were of the same type, it would work).

For example, it would be more logical to do:

void crear(NodoE **inicioptr,char id[5]) { // también vale char *id
   ...
   for (int i = 0; i < 5; i++) {
     NuevoPtr->ID[i] = id[i];
   }
}

or, if the values are strings ending in NULL (recommended)

      strcpy(NuevoPtr->ID, id);
    
answered by 15.07.2017 / 22:55
source