Pointers and Arrays

Pointers and Arrays


Pointer 

Pointer is a variable that store the address of another variable.

The syntax is : 

<type> *ptr_name;

Example (to initialize an integer pointer into a data variable):

#include <stdio.h>

int main ()
{
    int i;
    int *ptr;
    i = 2;
    ptr = &i;
    printf("%d\n", *ptr); // to print the value of *ptr = 5
    
    return 0;
}

Pointer to Pointer

A variable that saves another address of a pointer.

The syntax is :

<type> **ptr_ptr;

Example :

#include <stdio.h>

int main ()
{    
    int i;
    int *ptr, **ptr2;    
    i = 2;
    ptr = &i;
    ptr2 = &ptr;
    printf("%d\n", *ptr); // the output must be 2 because *ptr store the value of i
    printf("%d\n", **ptr2); // the output must be 2 because **ptr2 store the value of ptr    
    return 0;
}

Pointer Constant and Pointer Variable

- Pointer Constant is a pointer that can't be assigned with new value at run-time.
  Example : int x = 2;
- Pointer Variable is a pointer that can be assigned with new value at run-time.
   Example : int *ptr; // we can assigned value to the pointer


Array

An array is a collection of data items. Array is a pointer constant and array can be filled with pointer variable.
Array characteristics are homogenous (all elements in array have similar data type) and have random access (each element can be reached individually and doesn't have to be sequential).
All array index start from 0, which is for array dimensional value 3 will start from 0 to 2 and not 1 to 3.

The syntax is : 

(1 Dimensional Array)
<type> array_name[array_size];

(2 Dimensional Array)
<typearray_name[array_row][array_column];

(3 Dimensional Array)
<typearray_name[array_row][array_column][array_depth];

Example (to print value from array):

#include <stdio.h>

int main ()
{
    int i[3] = {2, 4, 6};
    int a;
    
    for(a = 0; a < 3; a++)
    {
          printf("The value of array i[%d] is %d.\n", a,  i[a]);
     }
    
    return 0;
}

Pointer to Pointer

A variable that saves another address of a pointer.

The syntax is :

<type> **ptr_ptr;

Example :

#include <stdio.h>

int main ()
{    
    int i;
    int *ptr, **ptr2;    
    i = 2;
    ptr = &i;
    ptr2 = &ptr;
    printf("%d\n", *ptr); // the output must be 2 because *ptr store the value of i
    printf("%d\n", **ptr2); // the output must be 2 because **ptr2 store the value of ptr    

    return 0;
}


Array of Pointer, Array of Character, and String

- Array of Pointer is an array filled with pointer(s).

  The syntax is : 
  <type> *array_name[array_size];

- Array of Character is an array filled with character(s).

   The syntax is :
    char array_name[array_size];

- String is an array of character that ended with null character.
  The syntax is the same as array of character.

String Manipulation : ( #include <string.h>)
1. strlen() = to find string length excluded null character.
2. strcpy(str1, str2) = to copy str2 to str1.
3. strncpy(str1, str2, n) = to copy first n characters of str2 to str1.
4. strcat(str1, str2) = to add string from str2 to the end of string str1.
5. strncat(str1, str2, n) = to add n characters of string str2 to the end of string str1.
6. strcmp(str1, str2) = to compare the value of string str1 and str2. returning 0 if true and        1 if false.
etc.

Char use %c and ' '
String use %s and " "
  

Princess Jesslyn Lorenza
2201750036

binus.ac.id
skyconnectiva.com


Comments