Arrays and pointers, Data Structure & Algorithms

C compiler does not verify the bounds of arrays. It is your job to do the essential work for checking boundaries wherever required.

One of the most common arrays is a string that is simply an array of characters finished by a null character. The value of any null character is o. A string constant is a one-dimensional array of characters terminated by a null character (\0).

For instance, consider the following:

char message[ ]= {'e', 'x', 'a', 'm', 'p', 'l','e','\0'};

Also, assume the following string that is stored in an array:


Given figure illustrates the way a character array is stored in memory. In the array each of character occupies one byte of memory and the last character is always '\0'. Note down that '\0' and '0' are not the same. The character array elements are stored up in contiguous memory locations.











                                          Figure: String in Memory

C concedes a fact that the user would employ strings very frequently and therefore provides a short cut for initialization of strings.

For instance, the string used above can also be initialized as char name[ ] = "sentence\n";

Note down that, in this declaration '\0' is not essential. C automatically inserts the null character.

Multidimensional arrays are described in the similar manner as one-dimensional arrays, apart from that a separate pair of square brackets is needed for each of subscript. Therefore a two-dimensional array will need two pairs of square brackets, a three-dimensional array will need three pairs of square brackets and so on.

In C the format of declaration of multidimensional array is given below:

data_type array_name [expr 1] [expr 2] .... [expr n];

where data_type is the type of array like char, int etc., array_name is the name of array & expr 1, expr 2, ....expr n are positive valued integer expressions.

The diagram of two-dimensional array of size 3 × 5 is illustrated in Figure



Figure: Schematic of a Two-Dimensional Array

In the particular case of a two-dimensional array, the given formula yields the number of bytes of memory required to hold it:

bytes = size of 1st index × size of 2nd index × size of (base type)

The pointers & arrays are closely associated. As you know, an array name without an index is a pointer to the primary element in the array.

Assume the following array:

char p[10];

p and &p[0] are similar because the address of the primary element of an array is the similar as the address of the array. Thus, an array name without an index produced a pointer. On the other hand a pointer can be indexed as if it were declared to be an array.

For instance, assume the following program fragment:

int *x, a [10];

x = a;

x[5] = 100;

* (x+5) = 100;

Both of the assignment statements place the 100 in the sixth element of a. In addition the (0,4) element of a two-dimensional array might be referenced in the given two ways: either through array indexing a[0][4], or through the pointer *((int *) a+4).

Generally, for any two-dimensional array a[j][k] is equivalent to:

*((base type *)a + (j * rowlength)*k)

Posted Date: 4/4/2013 6:25:35 AM | Location : United States

Related Discussions:- Arrays and pointers, Assignment Help, Ask Question on Arrays and pointers, Get Answer, Expert's Help, Arrays and pointers Discussions

Write discussion on Arrays and pointers
Your posts are moderated
Related Questions
In the previous unit, we have discussed arrays. Arrays are data structures of fixed size. Insertion and deletion involves reshuffling of array elements. Thus, array manipulation

The following are several operations on a AA-tree: 1. Searching: Searching is done using an algorithm which is similar to the search algorithm of a binary search tree. 2. Ins

Q. What do you understand by the term Binary Tree? What is the maximum number of nodes which are possible in a Binary Tree of depth d. Explain the terms given below with respect to

Explain the Assertions in Ruby Ruby offers no support for assertions whatever. Moreover, because it's weakly typed, Ruby doesn't even enforce rudimentary type checking on opera

Create main method or a test class that creates 2 Element objects that are neighbours of each other, the first element temperature set at 100, the 2nd at 0 and use an appropriate h

W h at are the different ways by which we can represent graph?  Represent the graph drawn below using those ways.     T he d iff e r e nt w a y s by

How many recursive calls are called by the naïve recursive algorithm for binomial coefficients, C(10, 5) and C(21, 12) C(n,k){c(n-1,k)+c(n-1,k-1) if 1 1 if k = n or k = 0

need c++ algorithmic software program to derive one numerical outcome from 10 levels of variables with 135 combinations cross computed

Define a B-Tree Justas AVL trees are balanced binary search trees, B-trees are balanced M-way search trees. A B-Tree of order M is either the empty tree or it is an M-way searc