Change of vector size in a structure

Asked

Viewed 1,104 times

3

I have the following structure:

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

It is possible to change the vector size of char "binary" of 5 for the size of a variable that would come as an input parameter for the structure (I don’t know if that’s possible too)? I need the vector size of the structure to change during the code but I don’t know how to make this change.

  • Does this change (structuring instants configuration) need to happen at compile time or runtime? All structure variables will always have the same vector size ?

  • 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

  • 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 name Implicantes I imagine someone is playing a hehe game

3 answers

4

If it were a vector in C++ it could. A array in C, as used, you cannot. If you are using C++, or switch to vector (in the case of char can use string) or C or C++ switches to pointer and it has all the consequences of this, like taking care of the allocation (it has an example in the answer of Anthony Accioly, but note that will have to take care of it always), maybe it is not what you want.

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

It’s even possible to have a array unique at the end of a no-size structure defined in C, but not compatible with all compilers, few people use, is not as recommended and ends up working as a pointer. Better not even try.

4

In the you usually solve this with a pointer of the type 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 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";

2


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

Browser other questions tagged

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