Update index.md (#34846)
This commit is contained in:
@ -19,8 +19,8 @@ int main(void) {
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
So, this looks a bit tedious. Up until now every variable created had some special role. But right now, it would be great if we could just store multiple values in one place and get access to the values with their place in the line maybe (first value, second etc.). Another way to look at this is, suppose you want to store a set of names, you need not create different variables for each name, instead you can create an array of names where each name has its unique identity or *index*. Also, we could use loops on them, which are things you will learn about later, but basically they do the same thing over and over again.
|
||||
eg. reading from the user, or printing out values.
|
||||
So, this looks a bit tedious. Up until now every variable created had some special role. But right now, it would be great if we could just store multiple values in one place and get access to the values with their place in the line maybe (first value, second etc.). Another way to look at this is, suppose you want to store a set of names, you need not to create different variables for each name instead you can create an array of names where each name has its unique identity or *index*. Also, we could use loops on them, which are things you will learn about later, but basically they do the same thing over and over again.
|
||||
eg. reading from the user or printing out values.
|
||||
|
||||
## Arrays in C
|
||||
Arrays are containers with a given size. They can store values of the **same type**. This is called base type of the array. You can access a value stored in the array with its *index*.
|
||||
@ -67,8 +67,8 @@ If you want to create an array with all the elements as `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.
|
||||
In this example `var` is going to store the value `1`.
|
||||
Here an int is created called `var`, and it is initialized to the 0th element of arr. **Very important 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`.
|
||||
|
||||
One for loop can be used to print the contents of an array.
|
||||
```C
|
||||
@ -88,12 +88,12 @@ int main() {
|
||||
```
|
||||
|
||||
## Array Stored in Memory
|
||||
If you creat an array locally, by default the values will stored in a `stack` data structure. If you want to store many more elements, like in millions or billions, a heap may be used. Heaps are expanded up to the total memory in the machine, but a stack only can store limited number of elements.
|
||||
If you create an array locally, by default the values will be stored in a `stack` data structure. If you want to store many more elements, like in millions or billions, a heap may be used. Heaps are expanded up to the total memory in the machine, but a stack only can store a limited number of elements.
|
||||
|
||||
An array declared globally or statically would have different storage specification from an array declared locally such as
|
||||
- A local array will be (usually) created on stack.
|
||||
- A local array will be (usually) created on the stack.
|
||||
- A global or static array will be (usually)created on bss/data segments.
|
||||
- A dynamically created array will be created on heap.
|
||||
- A dynamically created array will be created on the heap.
|
||||
|
||||
For declaring an array in heap we use the `malloc` function under the `stdlib` header file.
|
||||
|
||||
@ -123,9 +123,9 @@ 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};
|
||||
```
|
||||
|
||||
We can also declare the two-dimensional array without giving the number of rows, but number of columns is a must.
|
||||
We can also declare the two-dimensional array without giving the number of rows, but the number of columns is a must.
|
||||
|
||||
example: `int arr[][5]` - here the number of rows are variable
|
||||
example: `int arr[][5]` - here the number of rows is 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
|
||||
|
||||
@ -137,7 +137,7 @@ int arr[2][5] = {
|
||||
};
|
||||
```
|
||||
|
||||
Two nested for loops can be used to print the contents of a 2-dimensional array in tabular format.
|
||||
Two nested for loops can be used to print the contents of a 2-dimensional array in a tabular format.
|
||||
```C
|
||||
#include <stdio.h>
|
||||
|
||||
@ -168,7 +168,7 @@ int main() {
|
||||
|
||||
## Strings
|
||||
|
||||
To store strings/multiple characters, we use `char arrays` in C, because the language has no special type built in. One thing to be aware of, is that a terminating null(represented as '\0') is automatically added to the end, signaling that it is the end of the string. However, you may also initialze a string with curly braces `{}` as well, but you have to manually add the terminating null.
|
||||
To store strings/multiple characters, we use `char arrays` in C, because the language has no special type built in. One thing to be aware of is that a terminating null(represented as '\0') is automatically added to the end, signaling that it is the end of the string. However, you may also initialize a string with curly braces `{}` as well, but you have to manually add the terminating null.
|
||||
|
||||
Like so:
|
||||
```C
|
||||
@ -188,7 +188,7 @@ char* string = "I do not want to count the chars in this.";
|
||||
|
||||
## Typical mistakes, tips
|
||||
|
||||
- When you have an array filled with values and you want to make an another array that is exactly the same as the first, never ever do this:
|
||||
- When you have an array filled with values and you want to make another array that is exactly the same as the first, never ever do this:
|
||||
```C
|
||||
double first[] = {2,3,7};
|
||||
double second[] = first;
|
||||
@ -199,13 +199,13 @@ a = b;
|
||||
You can **only** deal with the values in an array one by one. You **cannot assign all at once**, when you learn about pointers later, the reasons will be clear.
|
||||
>(Basically, the first element of an array points to a memory address, and the elements after that are the "houses" next to that first one. So technically an array is just its first element's memory address. When you want to assign the second array the first array, you run into error due to differing types, or you are trying to change the second memory address of the first element in the second array.)
|
||||
|
||||
- When you want to create an array, you have to either tell its size, or assign values to it. Do not do this:
|
||||
- When you want to create an array, you have to either tell its size or assign values to it. Do not do this:
|
||||
```C
|
||||
int arr[];
|
||||
```
|
||||
The computer has to know how big of a storage to create for the array. Later on, you will learn about ways to create containers whose size are defined later. (Again, pointers.)
|
||||
The computer has to know how big storage to create for the array. Later on, you will learn about ways to create containers whose size are defined later. (Again, pointers.)
|
||||
|
||||
- When you index out of the array, the compiler is not always going to give you an error. This is called undefined behaviour, we just do not know what is going to happen. It could lead to your program crashing, simply slowing down, anything.
|
||||
- When you index out of the array, the compiler is not always going to give you an error. This is called undefined behavior, we just do not know what is going to happen. It could lead to your program crashing, simply slowing down, anything.
|
||||
```C
|
||||
int test[6];
|
||||
int a = test[-2];
|
||||
@ -214,15 +214,15 @@ int b = test[89];
|
||||
The reason for C not checking the indexing bound is simple: C is an efficient language. It was made, so your program is the fastest: communicates nicely with hardware etc. A nicely written C code does not contain indexing errors, so why would C want to check while running?
|
||||
|
||||
- When you try to access the last element of the array. Suppose the length of the array A be 4 and while accessing the last element as
|
||||
A[4] will return an error, as the the indexing starts from 0.
|
||||
A[4] will return an error, as the indexing starts from 0.
|
||||
|
||||
But, the disadvantage of array is that the memory required should be allocated before the run time of the program.
|
||||
But, the disadvantage of an array is that the memory required should be allocated before the run time of the program.
|
||||
|
||||
## Note
|
||||
In case of array any type of datatype can be used except 'void' and 'function'.
|
||||
In the case of array any type of datatype can be used except 'void' and 'function'.
|
||||
|
||||
### Declaration of Character Array and String
|
||||
When we use double quotes to intialize an array, the compiler takes it as a string, and adds a `'\0'` to the end of the array.
|
||||
When we use double quotes to initialize an array, the compiler takes it as a string and adds a `'\0'` to the end of the array.
|
||||
However, if we use single quote to initialize then there is no `'\0'` added to the end and the array behaves normally.
|
||||
|
||||
```C
|
||||
|
Reference in New Issue
Block a user