C Datastructures Networking OS

Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Saturday, 1 February 2014

TYPE COMPATIBILITY:
         C++ defines int,short int ,long int as three different types.Hence they must be cast when their values assigned to one another.
The types of values must be the same for compatibility or else a cats must be applied.

DIFFERENCES IN STORING CHAR CONSTANTS IN C AND C++:
     In C  char constant is considered as interger:
        for example,
                        sizeof('x') returns sizeof(int)
                         i.e size of ASCII value of x is returned
   Whereas in C++,char is not promoted to integer
                       sizeof('x')  returns sizeof(char)

Saturday, 20 April 2013

Usage of constant qualifier with pointer

Difference between  const * int ptr  and   int * const ptr...

First of all we need to understand what const qualifier is and how it is used.
            Declaration of variable with const qualifier specifies that its value will not be changed. It depends upon where const variables are stored ,we may change value of const variable by using pointer.The result is implementation-defined if an attempt is made to change a const.
                  #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                          const int a=100;
                          a+=100;                                //error
                          printf("value of a=%d",a);
                             
                  }
               
 o/p: assignment of read only variable 'a'
1)pointer to variable:-
          
                     int *ptr;
            In the above declaration the value of both variable and pointer can be changed.Both the pointer and the value pointed by pointer will be stored in read-write area of memory.

                 #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                           int a=100,b=200;
                           int *ptr;
                           ptr=&a;                                        //pointer to integer
                           printf("value of ptr=%d",*ptr);
                           ptr=&b;                                       //pointing to another variable
                           printf("value of ptr=%d",*ptr);
                           *ptr=500;                                   //changing the value of pointer
                            printf("value of ptr=%d",*ptr);
                  }
               
           o/p:
                     value of ptr=100;
                     value of ptr=200;
                     value of ptr=500;
2) Pointer to constant :
          const int * ptr;
                  or
          int const * ptr;

We change pointer to point to another variable,but we cannot change the value of the variable which is pointed by the pointer.Pointer is stored in read-write memory and variable is stored in read-only memory.
                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const int *ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         ptr=&j;                                             //valid
                         printf("value of ptr=%d",*ptr);
               
                        *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                 }
            
o/p:- error.assignment to read only location *ptr
3)Constant Pointer to variable:-

                int  *const ptr;

             We can change the value of variable pointed by the pointer, but we cannot change the pointer to point to another variable.

                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // valid.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 }     

         o/p: error in assignment to read only location ptr
4) constant pointer to constant variable:
              We cannot change both the value of variable pointed by the pointer and  also pointer to point to another variable.

                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const  int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 } 
 o/p:     error.assignment to read only location *ptr
            error in assignment to read only location ptr

Saturday, 9 March 2013

Program to find missing number in a sequence 1 to n...

Algorithm.

step 1: store  the sequence  in the array
step 2: calculate the sum of array;
step: 3: calculate sum of 'n' numbers from 1 to n using the formula n*(n+1)/2;
step 4 : subtract sum of array from sum of n numbers;
        ie  (n*(n+1)/2)-sum of array;
step 5: the difference is the missing number

this approach is better when there is a single missing number...then what about when there are 2 missing numbers...

its very simple
   for the numbers in a sequence the difference will be 1 for every pair
   so find the difference for every pair starting from index 0
   when the difference is greater than 1 then the ( minimum number in pair+1) is the missing             number
   thus we can find more than 1 numbers those are missed from the sequence.