Subscribe by Email


Tuesday, September 13, 2011

Some details about the types of pointers and arrays in C

Arrays are linear data structures whose data elements form a sequence. The elements of these linear structures are stored in memory as the contiguous memory locations and are represented as the same. These are the simplest data structures and are easy to be performed operations like insertion, deletion, searching, and traversal, sorting and merging. An array can store a finite number of data elements homogenous in nature i.e., they should be of same data type. The number of elements of the array represents the length or size of the array. The front and end index of the array are called lower bound and upper bound respectively. In C, the lower bound is always 0 and the upper bounds calculated as (size – 1). There are basically two types of arrays namely one dimensional arrays, two dimensional arrays and multi-dimensional arrays. The one –dimensional arrays are the simplest ones. Every name has a unique name. Each element of the array is represented by an index number or subscript of the element. An array is represented as:
Array name[ lower bound L, upper bound]

One - dimensional elements are implemented in C by allocating a sequence of contiguous memory location. The starting address of the first element of the array is called the “base address” of the array. C allows many searching algorithms namely linear search and binary search.
Two dimensional arrays are the arrays in which each element is itself an array. For example, a two dimensional array “A” can be represented as a table of M*N elements, where M is the number of rows and N is the number of columns. These arrays also like one dimensional array are stored as contiguous memory allocations. They must be linearized before storing.
These two dimensional arrays can be implemented in two ways i.e., row- major and column major. Row major implementation technique stores the arrays as rows whereas column major implementation technique stores arrays as columns i.e., it stores first row of the array as the first column in the memory. Algebraic Operations that can be performed on two- dimensional arrays are addition, subtraction, multiplication, division and transposition.
Pointer is a variable which stores the address of another variable. Pointers are the strongest as well as the weakest feature of the C programming language. Pointers are a mean to access and modify the address of a variable and its value. Pointers provide a way to implement dynamic allocation of memory and to improve the efficiency of program routines. But, they have a downside which is that they can cause your system to crash or hang if used in an incorrect way. There are no real arrays in a C program, but a chain of pointers. The name of the array is itself a pointer pointing to the first element of the array. There are many kinds of arrays. We will discuss them one by one. First is “array pointers”. These pointers store the address of the first element of the array. An array pointer can be declared as follows:
Int *a[10];
The second kind of pointers is “string arrays”. Strings as we know are nothing but just arrays of characters. A character pointer can be declared as:
Char name[ ]=”abc”;
Char *cpr;
The third type of pointer is called “const pointer” and can be defined as the constant pointer or a pointer to a constant. The fourth type of pointers is known as “structure pointers”. These pointers point to the structures. They too are declared by placing “*” in front of a structure name. Fifth type of pointer is called “object pointer” and points to an object the last and sixth type of pointer is the “THIS pointer” and it is used to automatically pass an implicit argument or a pointer to the object invoked in the function call.


No comments:

Facebook activity