Saturday 25 March 2017

Pointer



What are Pointers?

pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −
int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to Use Pointers?

There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations −

Pointer variables

In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
data_type* pointer_variable_name;
int* p;
EXAMPLE PROGRAM FOR POINTERS IN C:
#include
#include
int main()
{
   int *ptr, q;
   q = 100;
   /* address of q is assigned to ptr */
   ptr = &q;
   /* display q's value using ptr variable */
   printf("%d", *ptr);
   return 0;
}

POINTERS IN C – PROGRAM OUTPUT:

100

NULL Pointers

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program −
#include
int main () 
{
int  *ptr = NULL;
printf("The value of ptr is : %x\n", ptr  );
   return 0;
}
Output :
The value of ptr is 0.
-In most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.
To check for a null pointer, you can use an 'if' statement as follows −
if(ptr) /*succeeds if p is not null*/
if(!ptr) /*succeds if p is null*/
void Pointers:

we can declare a pointrer to be a of any data type void any cannot hold any value.so we have to type cast the pointer variable from one type to other type.


int x=27;

void *p;
p=&x;
printf("%d",(((int*)p));

Output :

The statement (int*)p makes p to become an interger type

Example:


#include stdio.h

#include conio.h
main()

{
     int x=27;
void*p;
p=&x;
printf("value is=%d",*((int *)p));
}

Output:

Value is 27

Pointer and Functions

1.Pointers can be passed as arguments to a functions definition.

2.Passing pointer to a functions is called call by value. 

3.In call by reference what ever changes are made to  formal arguments of the functions definition will affect the actual arguments in calling functions.

4.In call by reference the actual arguments must be pointers or references or addresses.

5.Pointer arguments are use full in functions,because they allow accessing the orginal data in the calling program.

Ex1 :
void swap(int*,int*);
main()
{
int a=10,b=20;

swap(&a,&b);
printf("%d,%d",a,b);
}
void swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}


Output: 20  10

Friday 24 March 2017

Patterns

Star Pattern 8

Program :


#include
#include

void main() 

{

int i,j;

clrscr();

    for(i=1;i<=10;i++)
{
    for(j=1;j<=10;j++)
                     {
printf(" * ");
<10 j="" p=""> }
printf("\n");
}
getch();
}



Output :

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Star Pattern 7

Program :

#include
#include
void main()
 {
int i,j,k,s=1;
clrscr();
for (i=1; i<=8; i++)
             {
for (k=s; k<=8; k++) 
               {
printf(" ");
}
             for (j=0; j< i; j++)
                             {
           printf("*");
                      }
s = s + 1;
printf("\n");
}
s = 1;
for (i=7; i>=1; i--) {
for (k=s; k>=0; k--)
                  {
printf(" ");
}
             for (j=i; j>=1; j--) 
                           {
              printf("*");
             }
s = s + 1;
printf("\n");
}
getch();
}

Output :
        
              *
            **
          ***
        ****
      *****
    ******
  *******
********
  *******
    ******
      *****
        ****
          ***
            **
              *












Monday 20 March 2017

C program to implement simple if-else in c

Syantax :

if(condition)
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}
else
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}

Program :

#include
void main()
{
    int num;
    printf(" <<< c program to implement simple if-else >>>\n\n");
    printf("\n Enter the number: ");
    scanf("%d",&num);
    if(num%2==0) 
    {
        printf("\n Entered number is Even Number \n");
    }
    else
    {
      printf("\n Entered number is Odd Number \n\n");
    }
}

Output :



C program for pre increment and post increment

Program :

#include
main()
{
 int i=10,j;

 j=i++; /* j value 10 , i value 11*/

 /*post increment*/

 printf("\n After post-increment: j value:  %d , i value: %d \n",j,i);

 j=++i; /* j value 12, i value 12*/

 /*pre-increment*/

 printf("\n After pre-increment: j value: %d, i value : %d \n",j,i);

 return(0);
}

Output :


C program for pre decrement and post decrement

Program :

#include
main()
{
 int i=20,j;

j=i--;          /* j value 20 , i value 21*/

 /*post decrement*/

 printf("\n After post-decrement: j value: %d , i value:%d \n",j,i);

 j=--i;         /* j value 22, i value 22*/
  
/*pre-decrement*/

printf("\n After pre-decrement: j value: %d, i value:%d \n",j,i);

 return(0);

}

Output :