Changing the size of the vector in a structure

3

I have the following structure:

struct Implicantes{
    int posicao;
    char binario[5];
    bool dontcare;
    bool tick;
    struct Implicantes *Next;
};

Is it possible to change the size of the vector of char "binary" from 5 to the size of a variable that would come as input parameter to the structure (I do not know if this is also possible)? I need the vector size of the structure to change during the code but I do not know how to make that change.

    
asked by anonymous 24.07.2016 / 20:44

3 answers

2

I'll cover a C ++ solution for your problem:

  

It is possible to change the size of the vector of char "binary" (...)

It is not possible to change the size of an array during the execution of the program (I used the term array instead of vector and I will clarify the reason). You will have to use another type of variable, as in the examples of the other answers. In C ++, there is a standard solution for when we want a variable number of elements of the same type (in this case, a variable number of char ), and is a class called vector :

#include <vector>

struct Implicantes{
  int posicao;
  bool dontcare;
  bool tick;
  struct Implicantes *Next;
  std::vector<char> binario;
};

Now yes: a vector, of variable size, of char . If this code sounds strange to you, I'm going to break it down a bit, but I think a full explanation goes beyond the scope of the question:

The std:: indicates that we are using a standard C ++ library element (std is an abbreviation of standard), these elements are all within namespace with that name. And <char> indicates what kind of variable the vector holds, you can create a vector<int> , vector<bool> , vector<Implicantes> , vector<qualquer_classe> .

In your example, the binario variable has size five, fixed, and space reserved for the five chars (which is why its size can not be changed), since the vector can have any size, and therefore does not have space reserved. Which brings the second part of the question ...

  

(...) of 5 for the size of a variable that would come as input parameter to the structure (I do not know if this is also possible)

I imagine you are new to the language and do not know builders. Constructors are exactly functions that receive parameters at the time of creating an object. For example:

struct Implicantes{
  int posicao;
  bool dontcare;
  bool tick;
  struct Implicantes *Next;
  std::vector<char> binario;
  //construtor recebe parâmetro e define tamanho do vetor [binario]
  Implicantes(unsigned int quantos_binario)
  {
    //resize é um método que altera o número de elementos do vetor
    binario.resize(quantos_binario);
  }
};

This function with the same structure name is a constructor, and now to create a Implicantes you will have to supply a number, as in the example:

int main() {
  //cria Implicantes com binario contendo 2 char
  Implicantes A(2);
  //cria Implicantes com binario contendo 7 char
  Implicantes B(7);
}

If you still want to be able to create a Implicantes without saying how many binario elements to have, you can declare a constructor without parameters, and this will be used when you do not provide any parameters:

struct Implicantes{
    int posicao;
    bool dontcare;
    bool tick;
    struct Implicantes *Next;
    std::vector<char> binario;
    //construtor recebe parâmetro e define tamanho do vetor [binario]
    Implicantes(unsigned int quantos_binario)
    {
        binario.resize(quantos_binario);
    }
    //construtor sem nenhum parâmetro, define tamanho de vetor [binario] para 5
    Implicantes()
    {
        binario.resize(5);
    }
};

And now ...

int main() {
  //cria Implicantes com binario contendo 2 char
  Implicantes A(2);
  //cria Implicantes com binario contendo 7 char
  Implicantes B(7);
  //cria Implicantes com binario contendo 5 char
  //através do construtor padrão
  Implicantes C;
}

After you have defined the size of the vector, just use it as usual with the arrays:

int main() {
  Implicantes C;
  C.binario[0] = '0';
  C.binario[1] = '1';
  C.binario[2] = 'z';
  //etc...
}
    
26.07.2016 / 21:49
4

In the you usually solve this with a type pointer char and malloc to reserve space dynamically:

struct Implicantes {
    int posicao;
    char *binario;
    bool dontcare;
    bool tick;
    struct Implicantes *Next;
}; 

struct Implicantes imp;
imp.binario = malloc(50 * sizeof *imp.binario);  
// 50 pode ser substituido por qualquer tamanho  

// ou ainda, se tudo vai ficar em escopo comum
// char s[50];
// imp.binario = s;

In the I would simply use a < a href="http://www.cplusplus.com/reference/string/string/"> string

struct Implicantes {
    int posicao;
    std::string binario;
    bool dontcare;
    bool tick;
    struct Implicantes *Next;
}; 

struct Implicantes imp = {};
std::string binario = "Essa e uma string chamada binario";
    
24.07.2016 / 21:10
4

If it were a vector in C ++ could. An array in C, as it was used, can not. If you are using C ++, or switch to vector (in the case of char you can use string ) or in C or C ++ changes to pointer and bear all consequences of this, such as taking care of the allocation (you have an example in Anthony Accioly's response, but note that you'll have to take care of this all the time), maybe it's not what you want .

In any case if you have to limit the size will have to be done manually. In C it is possible to create some facilities, but the consumer of this structure will have to deal with everything of their own volition. In C ++ it is possible to abstract all access control, but it gives a little more work.

It's even possible to have a single array at the end of a non-sizeable C structure, but it's not compatible with all compilers, few people use it, it's not recommended, . Better not even try.

    
24.07.2016 / 20:55