Wednesday, October 17, 2018

Pointers and Arrays

Pointer

Pointer is a variable that store the address of another variable
Syntax :
  <type> *ptr_name;
Two operators mostly used in pointer : * (content of) and & (address of)

Pointer to Pointer

Pointer to pointer is a variable that saves another address of a pointer
Syntax:
  <type> **ptr_ptr ;

Array
Data saved in a certain structure to be accessed as a group or individually. Some variables
saved using the same name distinguish by their index.
Syntax:
  type array_value [value_dim];

Array Initialization
Array can be initialized explicitly without dimensional value declaration


Accessing Arrays
Two analogous ways of accessing an element i=2;
  *(A+2) or A[2]
A  is equivalent with &A[0] or a constant pointer to the first element of particular array
To show A[2] on the monitor screen:
  printf(“%d”,A[2]) or
  printf(“%d\n”,*(A+2));


Pointer Constant & Pointer Variable
Pointer variable is a pointer that can be assigned with new value at run-time.
Pointer constant is a pointer that can not be assigned with new value at run-time
Array is Pointer Constant to its first element of the array. Array can be filled with pointer variable.

One Dimensional Array
Syntax 1D Array:
  type name_array[row];

Two Dimensional Array
Syntax 2D Array:
type name_array[row][col];

Three Dimensional Array
Syntax 3D Array :
type name_array[row][col][depth];

Array of Pointer
An array filled with pointer/s
Syntax :
type *array_name [value_dim];

Array of Character
Array filled with character/s
Syntax:
char array_name[value_dim];

String
String is an array of character that ended with null character ( ‘\0’ or in ASCII = 0)

String Manipulation
Types of function to manipulate string in string.h library
– strlen()
Return a value of string length; excluded null char
– strcpy(statement1, statement2)
Copy statement2 to statement1
– strncpy(statement1, statement2,n)
Copy first n characters of statement2 to statement1
– strcat(statement1, statement2)
Adding string statement2 to the end of string statement1
– strncat(statement1, statement2,n)
Adding n characters of string statement2 to the end of string statement1
– strcmp(statement1, statement2)
Comparing the value of string statement1 and statement2, if similar returning 0
– etc.

Thursday, October 11, 2018

Repetition


Repetition
·One or more instruction repeated for certain amount 
of time

·Repetition/looping operation:
– For
– While
– Do-while

             1. For

Syntax:
for(exp1; exp2; exp3) statement;
or:
for(exp1; exp2; exp3){
  statement1;
  statement2;
  …….
 }
exp1 :  initialization
exp2 :  conditional
exp3 :  increment or decrement
exp1, exp2 and exp3 are optional

EXAMPLE :
Program to print out numbers from 1 to 10

     #include<stdio.h>
int main()
{
    int x;
    for( x = 1 ;  x <= 10 ;  x++ ) printf( "%d\n", x );
    return(0);
}


      2. While
Syntax :

while (exp) statements;
or:
while(exp){
  statement1;
  statement2;
   …..
}

EXAMPLE :
int counter = 1;
while ( counter <= 10 ) {
     printf( "%d\n", counter );
     ++counter;
}


3. Do-While
Syntax :

do{
    < statements >;
} while(exp);

Keep executing while exp is true
exp evaluation done after executing the statement(s)

EXAMPLE :
int counter=0;

do {
     printf( "%d  ", counter );
  ++counter;
} while (counter <= 10);