Algoritma & Pemrograman – Pertemuan 5 (Review)

Pointer & Arrays

  • Pointer
    • Basically, variable that functions as a storage for another variable.
    • Syntax:

<type> *ptr_name;

  • Most used operator in pointer:
    • * (Content of)
    • & (Address of)
  • Example:

int i, *ptr;

ptr = &i;

 

(Using number)

int 3, *ptr;

ptr = &3; (This shows that the pointer stored the variable 3 inside it)

 

  • Pointer to Pointer
  • Basically, a pointer inception (Pointer that stores another pointer).
  • Syntax:

<type> **ptr_ptr;

  • Example:

int i, *ptr, **ptr_ptr;

ptr = &i;

ptr_ptr = &ptr;

 

(Using numbers)

int 5, *ptr, **ptr_ptr;

ptr = &5;

ptr_ptr = 5;

(This means that the pointer is pointed towards the “I” from the original pointer, which has the value of 5.)

 

 

  • Array(s)
    • Basically, data saved in a certain structure to be accessed in groups or individually.

Data(s) that were saved using the same name are distinguished by their index.

  • Characteristics of Array(s):
    • Homogenous
      • All elements have similar data types
    • Random Access
      • Doesn’t have to be in order. Can be accessed separately.
  • Syntax:

type array_value [value_dim];

  • Example:

int A[10];

  • Array(s) are consisted of these 4 components:
    • Type specified
    • Identifier (name of array)
    • Operational Index ( [ ]  )
    • Dimensional value inside operator [ ]
  • Keep in mind that these are still for one dimensional array. The next will be for above one dimension.

 

  • Array Initialization
    • Array(s) can be initialized without dimensional value declaration.
    • Example (No Dimension):

int B[ ] = {1, 2, -4, 8};

This means that the array has 4 elements.

1           2         -4           8

B[0]      B[1]      B[2]     B[3]

  • Example (With Dimension):

int B[8] = {1, 2, -4, 8};

1           2         -4           8           0           0           0           0

B[0]      B[1]      B[2]     B[3]     B[4]      B[5]      B[6]      B[7]

  • Example (Error):

int B[4] = {1, 2, -4, 8, 9}; -à Error

Error in result, caused by smaller dimension than given elements.

 

 

  • Accessing Array
    • There are two ways to access the element i = 2:

*(A + 2) or A[2]

  • A is equal to A[0] or a constant pointer to the first element of a particular array.
  • To show A[2] on the screen, there are two ways:
    • printf(“%d”, A[2]);
    • printf(“%d \n”, *(A + 2) );

 

  • Pointer Constant & Pointer Variable
    • Pointer Variable: Basically, a pointer that’s rewritable at run-time.
    • Pointer Constant: Basically, a pointer that’s untouchable at run-time.
    • Array is Pointer Constant to the first element to the array. Can be filled with Pointer Variable.
  • Example:

int x = 10, y = 20;

int *ptr;

ptr = &x;

ptr = &y;

(Note: ptr used in this code is Pointer Variable)

  • Pointer Constant can only be initialized at definition time.
  • C Compiler doesn’t limit the numbers of the possible dimensions. Instead, it’s limited by our PC’s memory.
  • Syntax 2D Array:

type name_array [row][col];

  • Example:

int a[3][4]

  • Initialization: Using rmo (Row Major Order)
  • Example:

int b[2][2] = {1, 2, 3, 4 };

int b[2][2] = { { 1, 2 }, { 3, 4 } };

int b[2][2] = { { 1 }, { 3, 4 } };

 

int x[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

int x[3][4] = { {1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

int b[2][2] = {1, 2, 3, 4 };

int b[2][2] = { { 1, 2 }, { 3, 4 } };

int b[2][2] = { { 1 }, { 3, 4 } };

int x[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

int x[3][4] = { {1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

  • Syntax 3D Array:

type name_array[row][col][depth];

  • Example:

int x[3][2][4] = {{{1,2,3,4}, {5,6,7,8}},

{{11,12,13,14}, {15,16,17,18}},

{{21,22,23,24}, {25,26,27,28}}

};

void main() {

int x[4][3][5] = {{{1, 2, 3}, {0, 4, 3, 4}, {1, 2}},

{{9, 7, 5}, {5, 7, 2}, {9}},

{{3, 3, 5}, {2, 8, 9, 9}, {1, 2, 1}},

{{0}, {1}, {0, 1, 9}}

};

printf(“%5d”, x[2][1][3]);

}

  • Array of Pointer
    • Basically, an array with pointers in it.
    • Syntax:

type *array_name [value_dim];

  • Example:

int i;

int *ptr[4];

int x=1, y=2,  z=3, w=5;

ptr[0]=&x, ptr[1]=&y; ptr[2]=&z;  ptr[3]=&w;

for(i=0;i<4;i++) printf(“%d “,*ptr[i]);

Output : 1   2   3   5

  • Array of Character
    • Basically, it’s like Array of Pointer, but instead of pointers, it has characters in it.
    • Syntax:

char array_name[value_dim];

  • Example:

char name[40];

char ss[20]={‘B’,’I’,’N’,’U’,’S’};  (20 elements)

char ss[ ]= {‘B’,’I’,’N’,’U’,’S’};   (5 elements)

  • String
    • Basically, the previous one (array of character) that ends with null character ( or in ASCII = 0)
    • String constant or String literal:
      • Characters that are read between double quotes.
      • Example: “Welcome to Binus”
    • String constant is pointer constant, thus can be assigned to an array of character:
      • Example:

char name[40] = ”Amir”;  //ok

name = ”Amir”;   // error name is a constant pointer

Name[40]= “Amir”;  //error

  • A String constant can be linked during compile-time:

“Hello, ” “world”

Is similar to:

“Hello, world”

  • Example of string initialization:

char s[ ] = “BiNus”;

Is similar to:

char s[ ] = {‘B’, ‘i’, ‘N’, ‘u’, ‘s’, ”}

  • String as a data type is not known in C.
  • Difference of Char & String
    • Character in C is written between single quotes. Each uses one byte of computer memory.
      • Example:char ch=’A’;char ch=65;     //ASCII

        char ch=0x41; //ASCII

    • String is written between double quotes.
  • String manipulation
    • In Standard Library Function (header file string.h) provides functions to manipulate string:
      • strlen()
        • Return a value of string length; excluded null char
      • strcpy(s1, s2)
        • Copy s2 to s1
      • strncpy(s1, s2, n)
        • Copy first n characters of s2 to s1
      • strcat(s1, s2)
        • Adding string s2 to the end of string s1
      • strncat(s1, s2, n)
        • Adding n characters of string s2 to the end of string s1.
      • strcmp(s1, s2)
        • Comparing the value of string s1 & s2, if familiar returning 0
      • etc.

For more info, please search at Google.com or Wikipedia.org, or at ASCII official website, or whatever website that teaches you coding. This one is quite a pain in the ass to write, so pardon the complicated language.

This entry was posted in BINUS University, Lessons, Review. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *