function using char and vector in c

Asked

Viewed 148 times

0

Good evening, I’m facing a problem.

I’m trying to fill a vector in C that way.

int a[5];
void lerValor(char grupo, int posicao, int valor) {
    grupo[posicao] = valor;
    printf("%i", grupo[posicao]);
}

I’m passing as parametric this example: lerValor('a',1,10);

Why doesn’t it work that way? Already agredeco the attention of all.

In my logic it would be: a[posica] = value; which in turn would be: a[1]=10;

  • In the above group code is not a vector.

  • but the group variable in one is of the char? type that receives the, then in my logic it would be: a[posica] = value; which in turn would be: a[1]=10;

  • If the variable is char then it has space for a single character. Note that char group is different from char group[].

  • What is the result obtained and what is the expected result?

  • it returns an error: error: subscripted value is neither array nor Pointer nor vector. really informs that it is not an array, matrix and vector, but how to solve this question? fill several vectors using a function, without using if and switch.

2 answers

1

There are some problems. The main thing is that your example reads Alor('a',1,10); it doesn’t help you understand what you want to do.

1) The variable grupo needs to be an array for integers. If you plan to initialize with an integer value, you cannot copy a char.

2) You need to pass the size of the array you want to initialize and not the position. If you want to update only one array position, just use the right command, you don’t need a method.

grupo[posicao] = valor;

For your example, it would be enough:

a[1] = 10;

3) If you want the method to initialize the entire array, you need to change the arguments and the name.

lerValor gives the impression that you want to recover the value of the array.

In this case, a call from initArray(a,1,10) would place the value "10" at position 0 and at position 1 of the array "a".

    void initArray(int grupo[], int tam, int valor) 
    {
            for(int i = 0; i < tam; i++)
            {
               grupo[i] = valor;
               printf("[%i]", grupo[i]);
            }
    }

4) I don’t know what you would be trying to print there with your printf.

  • In fact what the function of the OP is summarized in: char grupo, is the name of the vector variable to be changed, int posicao, which is the position of the vector to be modified and int valor, which is the value to be placed at that position. That is, suppose you have an array of integers called vet. The function lerValor('vet', 2, 10) change the position 2 of the vector vet for the value 10. OP only did not know how to express.

  • exactly. that’s what I want

1

First, the variable of grupo must be a vector to be able to use the bracket operator []. It must also be an array of integer values.

Second, if you want to pass the vector a as a function parameter lerValor() must use the variable name without quotation marks, i.e., a instead of 'a'.

Thus, the function definition is as follows::

void lerValor (int grupo[], int posicao, int valor) {
    grupo[posicao] = valor;
    printf("%i", grupo[posicao]);
}

And the function call, like this:

lerValor(a, 1, 10);
  • This is not what I want, in case I will have other vectors: a[5], b[5]... my idea was that this function would be responsible for filling values in these vectors. I’ll be creating another vector. what I want is to pass to this function the name of my vector(a, b...) and it fill my existing vector. without using if and switch.

  • I don’t know if I understand this correctly, but in this case, you can pass the address of your vector in the function parameter, and go walk with Pointer to the position that was passed to the function and assign the value to that memory position, so its original vector will have received that value at the specified position.

  • As an employee? You can post a code please?

Browser other questions tagged

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