Error: assignment of Member '' in read-only Object

Asked

Viewed 544 times

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;
}

}

Mistakes:

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 I have these mistakes I don’t understand. I know it has to do with ecoponto[pos].""="" but I don’t see why.

1 answer

2


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

Since you created a guy with typedef, could have used it in the parameter declaration. So it works, but it’s a waste. Could have declared ecoponto ecoponto[]. Or better yet, reverse the names on typedef, ideally the type names should be capitalized to avoid confusion with variable name, then the type would be Ecoponto.

  • Alias - is not creating "Ecoponto" as a type in typedef, and then using "Ecoponto" as a variable name?

  • @jsbueno too. Edited. Thank you for remembering.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.