Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Index notation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Implementation details== In the [[C (programming language)|C programming language]], we can write the above as {{code|*(base + i)}} (pointer form) or {{code|base[i]}} (array indexing form), which is exactly equivalent because the C standard defines the array indexing form as a transformation to pointer form. Coincidentally, since pointer addition is commutative, this allows for obscure expressions such as {{code|3[base]}} which is equivalent to {{code|base[3]}}.<ref>Programming with C++, J. Hubbard, Schaum's Outlines, McGraw Hill (USA), 1996, {{ISBN|0-07-114328-9}}</ref> ===Multidimensional arrays=== Things become more interesting when we consider arrays with more than one index, for example, a two-dimensional table. We have three possibilities: * make the two-dimensional array one-dimensional by computing a single index from the two * consider a one-dimensional array where each element is another one-dimensional array, i.e. an array of arrays * use additional storage to hold the array of addresses of each row of the original array, and store the rows of the original array as separate one-dimensional arrays In C, all three methods can be used. When the first method is used, the programmer decides how the elements of the array are laid out in the computer's memory, and provides the formulas to compute the location of each element. The second method is used when the number of elements in each row is the same and known at the time the program is written. The programmer declares the array to have, say, three columns by writing e.g. {{code|elementtype tablename[][3];}}. One then refers to a particular element of the array by writing {{code|tablename[first index][second index]}}. The compiler computes the total number of memory cells occupied by each row, uses the first index to find the address of the desired row, and then uses the second index to find the address of the desired element in the row. When the third method is used, the programmer declares the table to be an array of pointers, like in {{code|elementtype *tablename[];}}. When the programmer subsequently specifies a particular element {{code|tablename[first index][second index]}}, the compiler generates instructions to look up the address of the row specified by the first index, and use this address as the base when computing the address of the element specified by the second index. <syntaxhighlight lang="c"> void mult3x3f(float result[][3], const float A[][3], const float B[][3]) { int i, j, k; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { result[i][j] = 0; for (k = 0; k < 3; ++k) result[i][j] += A[i][k] * B[k][j]; } } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)