Referencing a specific element of a bidemensional matrix via pointer notation

Asked

Viewed 75 times

1

The C allows treating each row of a matrix of two dimensions considering them as a matrix of one dimension.

As we know, one way to reference two-dimensional matrices in the form of C pointers is, for example:

tabela[j][k] = *(*(tabela + j) + k)

According to my textbook:

Let’s assume the table matrix address is 1000, then table == 1000 and that table is a matrix [4][5]

As table is an integer matrix, each element occupies in this case 4 bytes. We have 5 elements in each row, so each row occupies 20 bytes. Since each row is a matrix of one dimension, each matrix of one dimension starts 20 bytes after the previous one

Therefore, table + 1 takes the table address (1000) and adds the number of bytes of the row (5 columns times 4 bytes per column, resulting 20 bytes)

Then, table + 1 is interpreted as the address 1020, which is the address of the second matrix of a dimension of the matrix, table + 2 is the address of the third and so on

How to reference an individual element in the line?

The address of the matrix is the same as the address of the first element of the matrix. It has already been determined that the address of the matrix formed by the third row is table[2] or table+2 in pointer notation.

The address of the first element of this matrix formed by the third row is &table[2][0] or *(table + 2) in pointer notation. Therefore, table + 2 and *(table + 2) would reference the same address, in this case, 1040.

However, as far as I know of pointers, the *deference operator returns the contents of a pointed address. Considering that table+2 is an address, so *(table + 2) should return me the content/value to which this address points, not 1040.

So, technically, the expression

tabela[j][k] = *(*(tabela + j) + k) 

would not make sense, because *(table + j) should return me a value, and not an address.

My doubt is why the expressions table + 2 and *(table + 2) would reference the same address, and the first one should return an address and the second the value of this address

  • But your question is about understanding how pointers work in two-dimensional matrices ? For obtaining values in a two-dimensional matrix is quite simple using array notation through indices with matriz[i][j]. And the same for the address of any matrix element with &matriz[i][j].

  • My question is based on understanding how the referencing of a specific element of a two-dimensional matrix works via pointer notation, which is table[j][k] = ((table + j) + k) because *(table + j) by pointer notations would return me a value, not an address

  • 2

    The mistake is in your interpretation of *(tabela + j). The guy returned for this is int* and not int as you assumed, because a 2d matrix works as if it were a pointer, although in practice it is not a pointer.

  • So it means that *(table + j) returns me an int * pointer that points to the address of the line j in question?

  • 1

    @Isac’s comment is correct...the statement int tabela[4][5] is interpreted syntactically as an array of 4 elements, where each element is the pointer to an array of 5 ints... tabela[2] is interpreted as being the pointer to the third array of 5 ints...in practice, the use of bi-dimensional (and multi-dimensional) arrays in C is very small, the normal is to work with pointer arrays,,,I don’t know why teachers insist on this business of "matrix" in C...

  • the expression *(tabela + j) is the same thing as tabela[j], that by definition is a pointer to the index "line"

  • Yes, my difficulty is in understanding why *(tabela + j) return the address of line j / of the 1st element of line j (in case 1040) and not the contents of this same element (that would be some integer, for example 30) Since when I study pointers I have as a basis that * is a deference operator that returns to me the content/value located at the address (pointer) operating *(tabela + j) return the value/content of the first element of the j line whose address is tabela + j I believe the difference is in the treatment with two-dimensional matrices

  • There lies the difficulty of understanding pointer notation for pointers

  • "by base that * is a deference operator that returns to me the content/value located at the address (pointer)" - Yes and so if you dereference a int** gets a int* (a pointer) and not a int (number). The content of tabela + j is a pointer and not a int.

Show 4 more comments
No answers

Browser other questions tagged

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