Skip to main content

Arrays

Amu_Ke_Fundye

Arrays


  • Array is a collection of similar elements having same data type, accessed using a common name.
  • Array elements occupy contiguous memory locations.
  • Array indices start at zero in C, and go to one less than the size of the array.

Declaration of an Array:
type variable[num_elements];
Example: int A[100];
  • It creates an array A with 100 integer elements.
  • The size of an array A can’t be changed.
  • The number between the brackets must be a constant.

Initialization of an Array:
  • int A[5]= {1,2,3,4,5}; /*Array can be initialized during declaration*/
  • int A[5]={1,2,3}; /* Remaining elements are automatically initialized to zero*/
  • int A[5]={1,[1]=2, 3,4,[4]=0};/* Array element can be initialized by specifying its index location*/

Problems with Arrays:
  • There is no checking at run-time or compile-time to see whether reference is within array bounds.
  • Size of array must be known at compile time.

Example-1: Read the 10 values into an array of size 10.
void main()
{
int A[10], i;
for (i=0; i<10; i++)
{
printf(“Enter the number %d”, i+1);
scanf(“%d”, &a[i]);
}

Example-2: Print the 10 values of an Array A.
void main()
{
int A[10], i;
for (i=0; i<10; i++)
{
printf(“%d “, A[i]);
}
}

Pointers & Arrays: Let a[10] be an array with 10 elements.
  • The name a of the array is a constant expression, whose value is the address of the 0th location.
  • An array variable is actually just a pointer to the first element in the array.
  • You can access array elements using array notation or pointers.
  • a[0] is the same as *a
  • a[1] is the same as *(a + 1)
  • a[2] is the same as *(a + 2)
  • a = a+0 = &a[0]
  • a+1 = &a[1]
  • a+i = &a[i]
  • &(*(a+i)) = &a[i] = a+i
  • *(&a[i]) = *(a+i) = a[i]
  • Address of an element i of array a = a + i * sizeof(element)

Multi-Dimensional Array
In C language, one can have arrays of any dimensions. Let us consider a 3 × 3 matrix
3 × 3 matrix for multi-dimensional array
To access the particular element from the array, we have to use two subscripts; one for row number and other for column number. The notation is of the form a [i] [j], where i stands for row subscripts and j stands for column subscripts.
We can also define and initialize the array as follows
Note: Two Dimensional Array b[i][j]
  • For Row Major Order: Size of b[i][j] = b + ( Number of rows * i + j )*sizeof(element)
  • For Column Major Order: Size of b[i][j] = b + ( Number of Columns * j + i )*sizeof(element)
  • *(*(b + i) + j) is equivalent to b[i][j]
  • *(b + i) + j is equivalent to &b[i][j]
  • *(b[i] + j) is equivalent to b[i][j]
  • • b[i] + j is equivalent to &b[i][j]
  • (*(b+i))[j] is equivalent to b[i][j]

Strings
In C language, strings are stored in an array of character (char) type along with the null terminating character “\0″ at the end.

Printf and scanf use “%s” format character for string. Printf print characters up to terminating zero. Scanf read characters until whitespace, store result in string, and terminate with zero.

Example:
char name[ ] = { ‘G’, ‘A’, ‘T’,’E’, ‘T’, ‘O’, ‘P’, ‘\O’};
OR
char name[ ] = “GATETOP”;
  • ‘\0′ = Null character whose ASCII value is O.
  •  ‘0’ = ASCII value is 48.
  • In the above declaration ‘\0′ is not necessary. C inserts the null character automatically.
  •  %s is used in printf ( ) as a format specification for printing out a string.
  • All the following notations refer to the same element:  name [i] ,  * (name + i),  * (i + name),  i [name]

Regards 
Amrut Jagdish Gupta

Comments

Popular posts from this blog

Undecidability

Amu_Ke_Fundye Undecidability Decidable Problem If there is a Turing machine that decides the problem, called as Decidable problem. A decision problem that can be solved by an algorithm that halts on all inputs in a finite number of steps. A problem is decidable, if there is an algorithm that can answer either yes or no. A language for which membership can be decided by an algorithm that halts on all inputs in a finite number of steps. Decidable problem is also called as totally decidable problem, algorithmically solvable, recursively solvable. Undecidable Problem A problem that cannot be solved for all cases by any algorithm whatsoever. Equivalent Language cannot be recognized by a Turing machine that halts for all inputs. The following problems are undecidable problems: Halting Problem:  A halting problem is undecidable problem. There is no general method or algorithm which can solve the halting problem for all possible inputs. Emptyness Problem:  Wh

Funny Shortcut to Remember Periodic Table

Amu_Ke_Fundye Funny Shortcut to Remember Periodic Table THE PERIODIC TABLE The periodic table is a tabular display of the chemical elements, organized on the basis of their atomic number's , electron configurations, and chemical properties.In order to appreciate the general trends in chemistry and to explain the deviations of some from these general trends we need to to have good knowledge about the position of those elements in periodic table. This knowledge is also extremely important for competitive exams. So, here I present a way to memorize this using some funny easy shortcut sentences. THE FIRST 18 ELEMENTS (H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar)  H ie  He   Li es  Be cause  B oron  C an N ot  O xide  Fl orine.. N e w  Na tions  M ight  Al so  Si ng  P eaceful  S ong  Cl early  A gain  THE S-BLOCK ELEMENTS (LEARN-ALONG THE GROUP) Group 1 (Li Na K Ru Cs Fr)  Sentence:  LiNa   K i  Ru by  C e  Fr iendship          

ALU and Data Path

Amu_Ke_Fundye ALU and Data Path The CPU can be divided into a data section and a control section. The   data section , which is also called the   datapath. Datapath The registers, the ALU, and the interconnecting bus are collectively referred to as the datapath.  Each bit in datapath is functionally identical.  The datapath is capable of performing certain operations on data items. The  control section  is basically the  control unit ,  which issues control signals to the datapath . Bus : A Bus is a collection of wires or distinct lines meant to carry data, address and control information. Data Bus : it is used for transmission of data. The number of data lines corresponds to the number of bits in a word. Address Bus : it carries the address of the main memory location from where the data can be accessed. Control Bus : it is used to indicate the direction of data transfer and to coordinate the timing of events during the transfer. PC (Pro