top of page
Search

Array and Pointer

  • Writer: welly widianto
    welly widianto
  • Oct 23, 2018
  • 1 min read

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:

int i, *ptr, **ptr_ptr;

ptr = &i;

ptr_ptr = &ptr;

To assign new value to i:

*ptr = 5; // means i=5 ;

**ptr_ptr = 9; // means i=9; or *ptr=9;



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.

Array characteristics:

–Homogenous

All elements have similar data type

–Random Access

Each element can be reached individually, does not have to be sequential


One Dimensional Array

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 [ ]

The illustration of one dimensional array



Array Initialization

Array can be initialized explicitly without dimensional value declaration –Example:

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

Array B has 4 elements


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));


Two Dimensional Array

Syntax 2D Array:

type name_array[row][col];

Example:

int a[3][4];


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];

Example:



 
 
 

Recent Posts

See All
File in C programming

There are large number of functions file I/O (Input Output) in C. In this blog you will learn to handle standard I/O in C using...

 
 
 
Cloud Computing

Cloud computing is shared pools of configurable computer system resources and higher-level services that can be rapidly provisioned with...

 
 
 

Comments


Post: Blog2_Post

081957023588

©2018 by Life on Campus. Proudly created with Wix.com

bottom of page