Sunday, November 3, 2013

Multidimensional Array in C Programming

,

Array is a variable that can hold more than one value of same type. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.)

Arrays are of two types:


Multidimensional arrays : When we create arrays of arrays known as multidimensional arrays. C Programming provide to create multidimensional array.A two dimensional array looks like matrix form.

Syntax :

<array_type> <array_name> [<number of rows>][<number of columns];

Example :

#include <stdio.h>
#include <conio.h>

int main ()
{
   int y[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
   int i, j;

   for ( i = 0; i < 5; i++ )
   {
      for ( j = 0; j < 2; j++ )
      {
         printf("y[%d][%d] = %d\n", i,j, y[i][j] );
      }
   }
   return 0;
}

Compile above and observe output.
Read more →

One Dimensional Array in C Programming

,

Array is a variable that can hold more than one value of same type. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.)

Arrays are of two types:


One Dimensional Array : Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program.

Syntax :

<array_type> <array_name> [<number of elements in array>];

Example: If the user want to store salary of 100 employee. This can be done by creating 100 variable individually but, by array it's now become simple.

int salary[100];


The following program initializes an integer array with five values and prints the array.

#include <stdio.h>
#include <conio.h>

int main()
{
     int a[]={9,8,7,6,5};
     int i;
     clrscr();
     printf("Array elements are\n");
     for(i=0;i<=4;i++)
     printf("%d\n",a[i]);
     getch();
     return 0;
}
Read more →

Saturday, November 2, 2013

Null Pointer in C Language

,

A Pointer is variable which contains the address in memory of another variable.value of a pointer variable is a pointer to some other variable. There is one other value a pointer may have: it may be set to a null pointer. A null pointer is a special pointer value that is known not to point anywhere. What this means that no other valid pointer, to any other variable or array cell or anything else, will ever compare equal to a null pointer. A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type.

To initialize a pointer to a null pointer (It is also possible to refer to the null pointer by using a constant 0), you might use code like

#include <stdio.h>
int *ip = NULL;

and to test it for a null pointer before inspecting the value pointed to, you might use code like

if(ip != NULL)
printf("%d\n", *ip);


Example :

#include <stdio.h>

int main()
{
 if(!NULL)
 {
  printf("I know preprocessor");
 }
 else
 {
  printf("I don't know preprocessor");
 }
 return 0;
}

Read more →