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