Today I learn about pointers and array in C programming. Pointers is a variable that store address of another variable. To make a variable become a pointer, just add * (asterisk symbol) before the variable's name. To indicate the address of a variable, we can use & (ampersand symbol). * means "content of"; & means "address of"
The syntax for Pointer :<data_type> *<pointer_name>;
To save another address of pointer in another variable, you can add another * (asterisk symbol)
Syntax :
<data_type> **<pointer_name> (double pointer)
<data_type> ***<pointer_name> (triple pointer)
<data_type> ****<pointer_name> (quadple pointer)
etc
bad example of not using pointer:
int angka1 = 3;
int angka2 = angka1;
int angka3 = angka2 = angka1;
int angka4 = angka3 = angka2 = angka1;
printf("%d", angka4); --> print = 3
example on using pointer :
int angka1 = 3;
int *angka2;
int *angka3;
int *angka4;
angka2 = &angka1;
angka3 = &angka1;
angka4 = &angka1;
printf("%d", angka2); --> print = 3
if these variable changed:
angka2 = &angka1;
angka3 = &angka2;
angka4 = &angka3;
it can cause error.
To make the program didn't error, we must add a pointer to pointer :
int *angka2;
int **angka3;
int *** angka4;
Array is data saved in a certain structure that can be used individually or as a group.
Syntax of array :
type array_name [dimensional_value];
sometimes array can also have its own element, the structure becomes:
type array_name [dimensional_value] = {element};
Accessing an array :
example: to access an element i = 2 in an array, can use :
*(A+2) (using a pointer) or A[2] (using an array)
Syntax of array according to its dimension:
One Dimensional Array = type array_name [row];
Two Dimensional Array = type array_name [row][column];
Three Dimensional Array = type array_name [row][column][depth];
Array of pointer = type *array_name[dimensional_value];
Array of character that ended with null character ("\0") also called string.
String always wrote between double quote (" ").
Difference between character and string is character use only single quote (' ') and can only contain one character when string use double quote (" ") and can contain many character.
To manipulate a string in c programming, we need to include <string.h> in Standard Library Function.