I will cover a C++ solution to your problem:
It is possible to change the vector size 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 make it clear why). You will need 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 your case, a variable number of char
), and it’s 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 seems strange to you, I’ll dig a little, but I think a full explanation falls outside the scope of the question:
The std::
indicates that we are using an element of the standard C++ library (Std is a standard abbreviation), these elements are all within a namespace
with that name. And the <char>
indicates what type of variable the array guard, you can create a vector<int>
, vector<bool>
, vector<Implicantes>
, vector<qualquer_classe>
.
In your example, the variable binario
has size five, fixed, and space reserved for the five chars
(which is the reason why its size cannot be changed), since the vector can have any size, and so does not yet have reserved space. Which leads to the second part of the question...
(...) from 5 to the size of a variable that would come as an input parameter for the structure (I don’t know if that’s possible either)
I imagine you’re new at language and don’t 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 need to provide 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 one Implicantes
without saying how many elements binario
must have, can declare a constructor without parameters, and this will be used when you do not provide any parameter:
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 set the vector size, just use normally as with the arrays:
int main() {
Implicantes C;
C.binario[0] = '0';
C.binario[1] = '1';
C.binario[2] = 'z';
//etc...
}
Does this change (structuring instants configuration) need to happen at compile time or runtime? All structure variables will always have the same vector size ?
– Intruso
Your program is in C or C++. They are different languages and therefore different solutions. For example,
bool
is something that does not exist in C– user5299
When I see these structures with a pointer called
Next
, I imagine it’s work for college/school and linked lists..., but when I see the nameImplicantes
I imagine someone is playing a hehe game– Kahler