Pointer and Array



Today, on Wednesday, 17th October 2018, we had lecture again in the auditorium at BINUS Anggrek.
Today's quizzes also require knowledge about pointer and arrays.

 Pointer 

Pointer is a variable that store the address of another variable.
syntax: *ptr_name(same as assigning variable);
For example:
int a=5;
int *ptr_a;
ptr_a=&a; //ptr_a refers to the address of a.
*ptr_a=10;
printf ("%d", a);

so the output will be  10, because we assigned a new value of ptr_a which is 10, and this also affect the value of a.

 Arrays 

Array is a structure data that gathers the same type of many data using the same name
syntax[dimensional_value]   (identifier as the name of the array.)

To initialize:
     int numbers[10] //array will provide 10 spaces, with the index starts from 0 to 9.
Another way to initialize an array without declaring the dimensional value:
     int number[]={2,4,5,3,1,...}

If you want to assign a value to certain indexes of array, you can do:
     A[5]=10; //the fifth 'box' of the array will be filled with 10.

To access an element in the array, you can do:
     printf ("%d",A[5]); or printf ("%d", *(A+5)); //accessing the second 'box' of the array

Array can be multidimensional. For example:
2D : type identifier[...][...] //first dimension refers to rows, second dimension refers to columns

No comments:

Post a Comment