Array & Pointer
In this session, I learned about array & pointer.
Array
Array is used to store the same type of data in a group. It means that all elements in the array have the same data type. To differentiate the elements in an array, they have an identifier called index. In C, index starts from 0. For example, if I want to make 3 integer variables: n1, n2, and n3; I can put those variables into an array n[index].
Here is an example of inputting elements in array :
int n[5] = {1,2,3,4,5}; -> this means that I can store 5 elements in an array called n.
To access the elements in an array, I just need to type the name of the array followed by the index, like n[0] = 1, n[1] = 2 and so on. I can also use pointer to access the elements(*(A+1) = 2).
The example above is what we called 1D array. There are also different dimensional array like 2D and 3D. For 2D, it is the same like 1D except that 2D array have rows(array[row][col]).
Example of 2D array :
int n[3][4] : { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
row 1 row 2 row 3
int n[3][4] : { {1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
Above are all valid ways to initialize array. To access the elements, type the name of the array followed by row index then column index (n[0][2] = 3).
In 3D, add depth in the array like height in dimension(array[row][col][depth]).
String
String is an array of character that end with null character(\0). Therefore, when making a character array, we need to allocate more than the number of characters. Example :
ITB is the same as char s[4] = {'I','T','B','\0'};
To manipulate string in C, library <string.h> is used.
Pointer
Pointer is a variable that points to another variable address. As I have learned, when declaring a variable, that variable is stored in a memory location. That location have an address, which is a number that differentiate between one variable and another. Therefore, pointer gives direct access to the value that is stored in the address that pointer variable pointed to. To declare a pointer, * is used. There are two types of pointer, pointer constant and pointer variable. Pointer constant cannot have new value at run-time, while pointer variable can. Example of pointer :
int x = 10;
int *ptr;
ptr = &x;
printf("x address : %d\n", &x);
printf("ptr address : %d\n", ptr);
printf("ptr value : %d\n", *ptr);
Output :
x address = 200
ptr address = 200
ptr value = 10
In this session, I learned about array & pointer.
Array
Array is used to store the same type of data in a group. It means that all elements in the array have the same data type. To differentiate the elements in an array, they have an identifier called index. In C, index starts from 0. For example, if I want to make 3 integer variables: n1, n2, and n3; I can put those variables into an array n[index].
Here is an example of inputting elements in array :
int n[5] = {1,2,3,4,5}; -> this means that I can store 5 elements in an array called n.
To access the elements in an array, I just need to type the name of the array followed by the index, like n[0] = 1, n[1] = 2 and so on. I can also use pointer to access the elements(*(A+1) = 2).
The example above is what we called 1D array. There are also different dimensional array like 2D and 3D. For 2D, it is the same like 1D except that 2D array have rows(array[row][col]).
Example of 2D array :
int n[3][4] : { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
row 1 row 2 row 3
int n[3][4] : { {1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
Above are all valid ways to initialize array. To access the elements, type the name of the array followed by row index then column index (n[0][2] = 3).
In 3D, add depth in the array like height in dimension(array[row][col][depth]).
String
String is an array of character that end with null character(\0). Therefore, when making a character array, we need to allocate more than the number of characters. Example :
ITB is the same as char s[4] = {'I','T','B','\0'};
To manipulate string in C, library <string.h> is used.
Pointer
Pointer is a variable that points to another variable address. As I have learned, when declaring a variable, that variable is stored in a memory location. That location have an address, which is a number that differentiate between one variable and another. Therefore, pointer gives direct access to the value that is stored in the address that pointer variable pointed to. To declare a pointer, * is used. There are two types of pointer, pointer constant and pointer variable. Pointer constant cannot have new value at run-time, while pointer variable can. Example of pointer :
int x = 10;
int *ptr;
ptr = &x;
printf("x address : %d\n", &x);
printf("ptr address : %d\n", ptr);
printf("ptr value : %d\n", *ptr);
Output :
x address = 200
ptr address = 200
ptr value = 10
Comments
Post a Comment