Information regarding pointers in array (#30573)

* Information regarding pointers in array

Added more Information in 2D arrays that we can declare the array without no_of_rows and usage of pointers in array element accessing

* fix: formatting/spelling

* fix: formatting, matched variable name to other examples
This commit is contained in:
athreya2013
2019-01-05 22:14:52 +05:30
committed by Christopher McCormack
parent d0879e5f59
commit a8801255c0

View File

@ -66,6 +66,7 @@ If you want to create an array with all the elements as `0`.
```C ```C
int var = arr[0]; int var = arr[0];
``` ```
Here an int is created called `var`, and it is initialized to the 0th element of arr. **Very importart to note** that in C, indexes start at zero as opposed to 1. This means that to access the first element, the index (between the brackets) is 0, to access the second element, the index is 1 etc. Here an int is created called `var`, and it is initialized to the 0th element of arr. **Very importart to note** that in C, indexes start at zero as opposed to 1. This means that to access the first element, the index (between the brackets) is 0, to access the second element, the index is 1 etc.
In this example `var` is going to store the value `1`. In this example `var` is going to store the value `1`.
@ -105,6 +106,12 @@ Two-dimensional arrays are common and can be initialized using the following syn
int arr[2][5] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int arr[2][5] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
``` ```
We can also declare the two-dimensional array without giving the number of rows, but number of columns is a must.
example: `int arr[][5]` - here the number of rows are variable
To access the element `arr[i][j]` we can also denote it by `*(arr+i*n+j)`,where `n` is the number of columns
It can be difficult to visualize a 2-dimensional array using the above syntax so developers often use optional, nested brackets to clarify the structure of the array. This is also a valid way to initialize a 2-dimensional array. It can be difficult to visualize a 2-dimensional array using the above syntax so developers often use optional, nested brackets to clarify the structure of the array. This is also a valid way to initialize a 2-dimensional array.
```C ```C
int arr[2][5] = { int arr[2][5] = {