Algorithm and Programming-Array and Pointers

ALGORITHM AND PROGRAMMING

ARRAY AND POINTERS


We know that our computer has some way to address memory and store data for us, so we can abstract that away and program at a slightly higher level with C, using variables and arrays instead of memory addresses ourselves.We initialize an array of characters (which is exactly what a string is) to store the value that we extract from the string,  if we wanted to initialize that array of characters manually, we can do something like:...

The 1st index of array starts from zero,so the last index is the number in initialization bracket minus 1.But if you want to input 3 characters,you must make at least 4 in the bracket of char of array,because the last index is always filled with '\0'

char initials[4];
initials[0] = 'D';
initials[1] = 'J';
initials[2] = 'M';
initials[3] = '\0';

OR

char initials[4] = {D,J,M};

Arrays can be used to store many of the same type of variable, but computers can only access one item in them at a time.The example above is for one dimensional array,there's also two and three dimensional array.For 2 dimensional array,as in matrix,there's row and column so it's written for example as initials[5][2],and as for 3 dimensional,we have an additional depth besides row and column,so it can be written like initials[5][2][3].

And in C, we call variables that store addresses of other variables pointers. The * symbol indicates that a variable is a pointer to some other variable type, so we could have int * in addition to char * and others.for example, int *a declares a pointer to an int with the name a, and later we use *a to go to the address a points to.If you want to point to an int *a,then you can upgrade the next variable to use double pointer(**),and so on.

Luis Indracahya
2201758934
luis.indracahya@binus.ac.id
skyconnectiva.com

Komentar

Postingan populer dari blog ini

FUNCTIONS(DATA AND VOID),RECURSION

Algorithm And Programming-Repetition

Algorithm and programming-review