Error: assignment of member '' in read-only object

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

typedef struct Ecoponto{
  int codigo;
  int contentores[3];
  char *cidade;
  char *rua;
  int nporta;
}ecoponto;



int insereEcoponto (const struct Ecoponto ecoponto[],int tam,int pos,int codigo,int porta,char* cidade,char* rua){



for(pos=0;pos<tam;pos++){
    if(ecoponto[pos].codigo == codigo){
        return 0;
    }
}




if(pos<tam){
    ecoponto[pos].codigo = codigo;
    ecoponto[pos].nporta = porta;
    ecoponto[pos].cidade = cidade;
    ecoponto[pos].rua= rua;
    ecoponto[pos].contentores[0] = 0;
    ecoponto[pos].contentores[1] = 0;
    ecoponto[pos].contentores[2] = 0;
}else{
    printf("Vetor cheio.\n");
    return 0;
}

}

Errors:

main.c:41:30: error: assignment of member 'codigo' in read-only object
     ecoponto[pos].codigo = codigo;
                          ^
main.c:42:30: error: assignment of member 'nporta' in read-only object
     ecoponto[pos].nporta = porta;
                          ^
main.c:43:30: error: assignment of member 'cidade' in read-only object
     ecoponto[pos].cidade = cidade;
                          ^
main.c:44:26: error: assignment of member 'rua' in read-only object
     ecoponto[pos].rua= rua;
                      ^
main.c:45:38: error: assignment of read-only location '(ecoponto + (sizetype)((long unsigned int)pos * 40ul))->contentores[0]'
     ecoponto[pos].contentores[0] = 0;
                                  ^
main.c:46:38: error: assignment of read-only location '(ecoponto + (sizetype)((long unsigned int)pos * 40ul))->contentores[1]'
     ecoponto[pos].contentores[1] = 0;
                                  ^
main.c:47:38: error: assignment of read-only location '(ecoponto + (sizetype)((long unsigned int)pos * 40ul))->contentores[2]'
     ecoponto[pos].contentores[2] = 0;
                                  ^

Why do I have these mistakes I do not understand. I know it has to do with ecoponto[pos].""="" but I do not see why.

    
asked by anonymous 02.01.2017 / 15:58

1 answer

2

You have declared the parameter as const so you can not modify it. If the intent is to modify, you should not use this modifier. You may have other problems in the code, but these specific errors are generated by this.

Since you created a type with typedef , you could have used it in the parameter declaration. That works, but it's a waste. Could have declared ecoponto ecoponto[] . Or better yet, it reverses the names in typedef , ideally type names must be uppercase to avoid confusion with variable name, there the type would be Ecoponto .

    
02.01.2017 / 16:04