Is it possible to assign the first column of a row of a/a vector/matrix?

Asked

Viewed 91 times

1

taking as an example a two-dimensional vector of the type char 2x2:

char vet[2][2];

Is it possible to assign to the first line indices of this vector? or they work like a pointer to the other rows and columns?

I tried to do it this way

#include <stdio.h>
#include <string.h>


int main(void){
    char v2[2][2];
    v2[0] = 'a'; // LINHA 10
    v2[1] = 'c'; // LINHA 11
    printf("%c\t%c", v2[0], v2[1]);
    return 0;
}

compiler is returning me 2 errors on line 10 and 11

assignment to Expression Whit array type

finally the code does not compile

  • 2

    I think the compiler disagrees about who’s on line 10...

  • My previous comment was obsolete after editing =]

  • 1

    that I had been doing a lot of editing on the code went unnoticed

2 answers

1


Assanges, you have to keep in mind that when you are working with matrices you need to pass the full address of the position to which you want to make an assignment. The declaration and use of matrix and vector are different.

char vet[2]; //Isso é um vetor de duas posições
char matriz[2][5]; //Isso é uma matriz com 2 linhas e 5 colunas

Therefore, conceptually we can represent the disposition of the elements in memory in the following way:

inserir a descrição da imagem aqui

A fun Fact about the matrix that we normally don’t need to worry about in PC programming is that although there is this "theoretical" arrangement of the elements in matrix format, the elements are actually arranged in memory spaces sequentially, i.e., for the above example matrix[ 1 ][ 0 ] in memory comes right after matrix[ 0 ][ 4 ]

Another fact about matrices is that when you reference it in this way "matrix[ 1 ]" you are pointing to the value of the memory address. You can test with the example below:

#include <stdio.h>
#include <string.h>


int main(void){
    char v2[2][5];
    printf("%d", v2[0]); //Imprimirá um número X
    printf("\n%d\n", v2[1]); //Imprimirá um número X+5, pois a linha 0 tem 5 colunas

    return 0;
}

But anyway, to assign a value to a position of the matrix you need to pass the position completely. Example:

char matriz[2][3];
matriz[1][2] = 'a';

The result would be:

inserir a descrição da imagem aqui

  • Gabriel, do you have the reference that the matrix is continuously raised? I also believe this, but I could not recover this reference when I wrote my reply, so I thought it best not there

  • @Gabriel Magri I just don’t think that this kind of illustration of a matrix would work for a three-dimensional matrix eg matrix[3][4][6]

  • @Assanges, exactly, this representation is for a two-dimensional matrix only.

  • @Jeffersonquesado, you can take the test to prove it in an experimental way, as I mentioned there in the answer. Herbert Schildt’s book "Full and Total C" explains this in a different way on page 106. It says that, for example, in a matrix a of 10 by 10 the element [0][4] can be referenced in two ways, a[0][2] or *(a+2) while the element [1][2] can also be referenced in two ways, a[1][2] or *(a+12).

  • C cometo e total? Okay, there’s a challenge on the internet that was trying to find a page of it without errors. Further reference: http://www.seebs.net/c/c_tcn3e.html http://www.seebs.net/c/c_tcn4e.html#pages https://www.quora.com/Which-bookis-better-Herbert-Schildts-C-The-completereference-Ritchie-and-KerninghansThe-programming-language/answer/Madhusudhan-Srikkanth?share=ad697c76&srid=hPI1b; if this piece of information is not defined by the language, this means that any uncompressed compiler, which means that this experiment has serious methodological flaws

  • @Jeffersonquesado interesting. I will take a look later calmly.

Show 1 more comment

0

When you put v2[0] or v2[1], you are not yet referring to a character. To refer to a character, you need to put the two indexes, such as v2[0][0], or v2[1][0].

The guy char[2][] needs two indexes to be transformed into char. Only by a single index will get a reference to a memory region that can not have attribution, so he complains that the attribution to a array cannot occur.


By the way, the messages seem to refer to the lines:

v2[0] = 'a';
v2[1] = 'c';

since in the print line has no attribution in it.

  • good but when I set v[1] and try to assign handle and when I set vet[1][1] and try to assign only to the first vet type[0] = 'c'; does not handle pq?

  • @Sorry Assanges, I don’t understand.

  • char v1[2]; v1[0] = 'a'; versus char v2[2][2]; v2[0][0] = '1';. Something to that effect?

  • yes these two examples above work but if I try to assign only to the first raw index does not take into account the above example: char v2[2][2]; if I try to assign only to the index does not work type v2[0] = '1'; does not take now if I do v2[0][0] = '1'?

  • Exactly the reasons that this happens I have some doubts, I did not want to put as an answer not to speak wrong. If I’m not mistaken, when you create a multidimensional matrix, you allocate a single memory space and the position 1,0 of this matrix is transformed into an addition of an offset to the matrix address. Type, in this example of two columns per row, the address would be addr(v2) + (1 * 2 + 0), where the (1 * 2 + 0) would be offset

  • 1

    @Assanges, see this link : https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm. ; here you have the following example: int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};, indicating that the matrix in C is in fact a continuous set of memory and that the position 2,3 in the matrix equals to the position 2*4 + 3 = 11

  • intriguing really

Show 2 more comments

Browser other questions tagged

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