Why do you use a vector with a name on it?

Asked

Viewed 104 times

5

would like to know the function of using a name within a vector?

char chave[NUM_LETRAS + 1]; 
int frequencias[NUM_LETRAS];
int frequencias2[NUM_LETRAS];
char codificado[MAX_STR];
char descrip[MAX_STR];    
  • 1

    normally this "name" is a variable that has a value, for example at the beginning of the code there should be MAX_STR = 255;

  • and always have to be capitalized the names? because I tried to change and did not give

  • 1

    has to be the same that has been declared, ie, int max_str = 255; you should use minuscule

  • fgets(encoded, MAX_STR, stdin); and that function?

3 answers

4

Generally capitalized names denote constant values, values that will not change during the course of the program, but nothing prevents these values from having a lower-case name as well. It is only a matter of good practice. A brief example of use:

#define NUM_LETRAS 50
#define num_letras 25
#define MAX_STR 255

#include <stdio.h>

int main() {
    /* Aqui, o compilador troca NUM_LETRAS pelo valor definido acima,
     * criando um vetor de caracteres de 51 letras (50 + 1)
     */
    char chave[NUM_LETRAS + 1];

    /* Já aqui, o compilador troca num_letras por 25,
     * criando um vetor de caracteres de 26 letras (25 + 1)
     */
    char chave2[num_letras + 1];

    // O mesmo ocorre aqui
    char codificado[MAX_LEN];

    /* fgets() lê de uma fonte de entrada de dados 
     * (no caso 'stdin') o número MAX_STR (255) de letras
     * e coloca dentro do vetor 'codificado'.
     */
    fgets(codificado, MAX_STR, stdin);

    return 0;
}

Constants are useful when the same number that always refers to the same thing during the code is repeated several times. This facilitates code changes, rather than searching for all 255 values of the program that refer to the maximum length of a string, it simply changes the set value to MAX_STR.

Consequently, the code becomes more readable, because this solution avoids the use of "magic numbers" (numerical values that nobody knows where it came from and to which it refers), replacing explicit values without description by self-explanatory names.

3

You seem to have confused some things. In C/C++:

  • t foo[bar]

Represents the declaration of a variable foo type t, allocating a continuous addressing space (vector), and finally assigning the address of the first position of this vector to the variable foo, size bar. Soon, we will have bar type variables t continuously allocated, and the pointer value foo will be the memory address of the first position of this vector.

In his example:

char chave[NUM_LETRAS + 1];

It means we have a vector, storing char-type variables of size NUM_LETRAS + 1. In this case, the name NUM_LETRAS probably represents a preprocessing macro defined via #define (#define NUM_LETRAS 10 for example).

1

This avoids the declaration of unnecessary positions in a vector, as the user himself has the option to enter the amount of spaces desired. An example would be the registration of names in a vector:

#include <iostream> 
using namespace std; 
int main() 
{
    int tam; // <- variável que armazena o tamanho do vetor.

    cout<<"Digite a quantidade de nomes que deseja inserir: ";
    cin>>tam; // <- usuário digita a quantidade de posições desejada.

    string nomes[tam]; // <- vetor de tamanho definido pelo usuário.


    ... // segue código.
  • Recalling that Vlas support (variable length array) is optional in C11, and has been removed completely in C++.

Browser other questions tagged

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