Struct with char vector

Asked

Viewed 195 times

0

So guys, I created this struct

typedef struct{
    int inicio;
    int tamanho;
    int fim;
    char *elementos[50];
}Fila;

And I intend to create a char vector, just as I have created several times an integer vector. Only it is not working at the time of implementation of the queue creation function.

Fila* criarFila(int tamanho){
    Fila* novaFila = new Fila;
    novaFila->tamanho = tamanho;
    novaFila->inicio = -1;
    novaFila->fim = -1;
    novaFila->elementos = new char[tamanho];

    return novaFila;
}

When it comes to compiling, it makes the mistake:

listar.cpp:66:22: error: incompatible types in assignment of ‘char*’ to   ‘char* [50]’
  novaFila->elementos = new char[tamanho];
                      ^

Does anyone understand why you are making this mistake?

  • Try to trade char *elementos[50]; for char elementos[50];

1 answer

0

char *elementos[50];

So you are creating an array of 50 char pointers. Probably what you want is just a pointer (then the new char[size] will work):

char *elementos;

Or, if you want a fixed size array:

char elementos[50];

In this case, it will be allocated together with the struct (no need to use new).

Browser other questions tagged

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