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.

No comments:

Post a Comment