Langsung ke konten utama

Pointers and Arrays

POINTERS AND ARRAYS


SUB TOPICS :
  • Pointer Definition
  • Pointer Concept
  • Pointer to Pointer
  • Array
Pointer Definition
       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)
       Example:
Initialize an integer pointer into a data variable:
int i, *ptr;
ptr = &i;
To assign a new value to the variable pointed by the pointer:
*ptr = 5;  /* means i=5 */


Pointer to Pointer

·         Pointer to pointer is a variable that saves another address of a pointer

·         Syntax:
§  <type> **ptr_ptr ;
·         Example:
o   int i, *ptr, **ptr_ptr;
o   ptr = &i;
o   ptr_ptr = &ptr;
o   To assign new value to i:
o   *ptr = 5;                      // means i=5 ;
o   **ptr_ptr = 9; // means i=9; or *ptr=9;

Array Definition
       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.

       Array characteristics:
      Homogenous
            All elements have similar data type
      Random Access
            Each element can be reached individually, does not have to be sequential
        Syntax:
                        type array_value [value_dim];
       Example :
                        int A[10];
       The definition consists of 4 components:
      Type specified
      Identifier (name of the array)
      Operator index ([  ])
      Dimensional value inside operator [ ]



Array Initialization
       Array can be initialized explicitly without dimensional value declaration

      Example:
            int B[ ]={1, 2, -4, 8};
                                    Array B has 4 elements
      Example:
            int B[8]={1, 2, -4, 8};

Accessing Array
        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 and 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.
       Example:
      int x=10, y=20;
      int *ptr; //ptr is pointer variable
      ptr = &x;
      ptr = &y;

Two Dimensional Array
/* Printing out array 2-D */
#include <stdio.h>
void main() {
                        int two_dim[3][5] = {1, 2, 3, 4, 5,
                                                      10, 20, 30, 40, 50,
                                                      100, 200, 300, 400, 500};
                        int i, j;
                        for (i=0; i<3; i++){
                                    for (j=0; j<5; j++) printf("%6d", two_dim[i][j]);
                                    printf("\n");
                        }
}

String
       String is an array of character that ended with null character ( ‘\0’ or in ASCII = 0)
       String constant or string literal is some characters written between double quote
      Example: ”Welcome to Binus”
      String constant type 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

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 and s2, if similar returning 0
      etc.

Summary
       Pointer is a variable that store the address of another variable
       Pointer to pointer is a variable that saves another address of a pointer
       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 which called as an array
       String is an array of character that ended with null character

2201760945
Skyconnectiva
Kevin Orlando Sutanto

Komentar

Postingan populer dari blog ini

Sorting & Searching

Sorting & Searching SUB TOPICS : Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort Linear Search Binary Search Interpolation Search Sorting •        Sorting needs to speed up searching operation in a list. •        Sorting type: –       Ascending –       Descending Sorting algorithm: 1. Internal sorting     All data to be sorted are loaded to RAM 2. External sorting     Sorting process using secondary storage •        Simple: –                       Bubble sort –                       Selection sort –          ...

Cloud Computing Services

Cloud Computing Services What is Cloud? Cloud refers to a network or internet, which is present at certain place which is accessible from any location over public network or private network Cloud Computing refers to manage, configuring, and accessing the applications online. It offer online data storage, compute, network infrastructure and application which delivered as a network services. Cloud computing tends to separate infrastructure with business, whereas terms "infrastructure" refer to hardware, networking, and software which managed as one entity. And terms "Business" refers to procedural workflow, strategic enforcement, etc Deployment Examples Social networking : Facebook, Instagram, LinkedIn, etc. Data sharing / collaboration : Email, Dropbox, etc. Education : Quipper, Smart Campus, E-Learning, E-Library, etc. Business : Office application : Online shop portal, Google doc, Personal / Corporate                  ...

File Processing

FILE PROCESSING SUB TOPICS : Files and Streams File Definition Open File Close File Input File Output File Files and Streams Streams Definition •       To keep key in data from keyboard need to be saved at secondary storage device as a data file. •       Stream is a sequence of character. All input and output data is a stream. C sees file as a stream. •        When a C program run, there are three (3) standard streams activated: 1. Standard Input Stream     Controlling input stream from keyboard 2. Standard Output Stream     Controlling output stream to the monitor 3. Standard Error Stream     Controlling the error messaging •        Each stream associated with a file. File Definition •        File is a collection of record •    ...