31 Aug 2017

What is the output for this code and give clear explanation?

#include<stdio.h>
#include<stdlib.h>
int main()
{
 char *a = "India";
 char b[] = "India";
 a = a+1;//line 5
 b = b+1;//line 6
/ *assume printf */
}


28 Aug 2017

Difference between Shallow copy and Deep copy

An object copy is a process where a data object has its attributes copied to another object of the same data type.
Shallow Copy :
  1. Shallow copying is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. A shallow copy of an object is a new object whose instance variables are identical to the old object.
  2. The situations like , if you have an object with values and you want to create a copy of that object in another variable from same type, then you can use shallow copy, all property values which are of value types will be copied, but if you have a property which is of reference type then this instance will not be copied, instead you will have a reference to that instance only.
  3.  A shallow copy can be made by simply copying the reference.
public class Ex {

    private int[] data;

    // makes a shallow copy of values
    public Ex(int[] values) {
        data = values;
    }

    public void showData() {
        System.out.println( Arrays.toString(data) );
    }
}

public class UsesEx{

    public static void main(String[] args) {
        int[] vals = {-5, 12, 0};
        Ex e = new Ex(vals);
        e.showData(); // prints out [-5, 12, 0]
        vals[0] = 13;
        e.showData(); // prints out [13, 12, 0]
        // Very confusing, because I didn't intentionally change anything about the 
        // object e refers to.
    }

}

Deep Copy : 
  1. Deep copy is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit by bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed. A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old.
  2. A deep copy means actually creating a new array and copying over the values.

public class Ex{
    
    private int[] data;

    // altered to make a deep copy of values
    public Ex(int[] values) {
        data = new int[values.length];
        for (int i = 0; i < data.length; i++) {
            data[i] = values[i];
        }
    }

    public void showData() {
        System.out.println(Arrays.toString(data));
    }
}

public class UsesEx{

    public static void main(String[] args) {
        int[] vals = {-5, 12, 0};
        Ex e = new Ex(vals);
        e.showData(); // prints out [-5, 12, 0]
        vals[0] = 13;
        e.showData(); // prints out [13, 12, 0]
        // Very confusing, because I didn't intentionally change anything about the 
        // object e refers to.
    }

}

22 Aug 2017

Dangling & Function pointers in C

Dangling pointer :-

Which pointer variable pointing to an inactive or dead location, it is called a dangling pointer.

Function pointer :

Which pointer variable holds the address of function, it is called function pointer

Advantage:-

By using function pointers in C, we can pass a fun as an argument to another function

  • By using function pointers in C, it is possible to access or modify the local data of a function from outside.
  • function pointer call 2 are faster than normal f’n call
syntax :  datatype*(*ptr)(); – non parameterized
ex: int*(*ptr)();
datatype*(*ptr)(datatype);  – > parameterized
ex : int*(*ptr)(int);
  • Depends on the function pointer head only, we require to decide the parameterized or not parameterized function.
  

Written by Call by value , written by address :

  • Whenever of fun’s is returning value type data then it is called “written by value”
  • Whenever a function is returning address type data then it is called written by address, i.e function returning pointer type.
  • The basic advantage of return by address is we can modify or access local data type of a function from outside.
  • Whenever a function is returning an integer value the specify the return by address is we can modify or access local data of function from outside.
  • Whenever a function is returning an integer value then specify the return type as an into i.e function
  • The basic function advantage of return by address is we can modify or access local data of a function from outside
  • Whenever a function is returning an integer value then specify the return type as an int. i.e, function retuning value(return by value ).
  • Whenever a function is returning an integer variable address ( is we can modi) then specify the return type as an int
i.e, ( function returning pointer (return by address)

int *abc()
{
int a=10;
++ a;
return &a;
}
void main()
{
int *ptr;
ptr = abc();
printf(“\n value of a%d”, *ptr);
}

o/p : value of a : 11(illegal)

  • According to storage classes of c, a is a auto variable which is constructed with in the abc() function so when the control will pass to main() function it will destroyed
  • In above program ptr is called dangling pointer because it is pointing to inactive or dead location
  • The solution of the dangling pointer is in place of creating auto reconsidered to create static variable because lifetime of the static variable is entire the program.

int *xyz()
{
static int s=5;
–s;
return &s;
}
void main()
{
int *ptr
ptr=xyz();
print=xyz();
printf(“\n static data: %d”,*ptr);
o/p : static data : 4

void abc()
{
printf(“welcome abc”);
}
void main()
{
void (*ptr)();       // function pointer
ptr=&abc;
ptr(); //abc();
}
o/p : welcome abc

void abc()
{
printf(“welcome abc”);
}
void main()
{
void(*ptr)();
ptr=&abc;
abc();
}
o/p : welcome abc

int xyz (void)
{
int a=10;
++a;
return a;
}

void main()
{
int(*ptr)(void);
int x;
ptr=&xyz;
x=ptr();
printf(“value of : %d”,x);
}
o/p : value of x : 11

int abc()
{
static int s=15;
++s;
return &s;
}
}
void main()
{
int*(*ptr)();         // function pointer
int *p;        //      pointer integer
ptr=&abc;
p=ptr();
int abc(int x)
{
static  int s;
s=++x;
return &s;
}
void xyz(int i)
{
printf(“\n static data is xyz : %d”,i);
}
void fun ptr(int * myptr)
{
myptr= 88;
}
void main()
{
int *(*ptr) (int);
int +*p;
int +a;
ptr=&abc;
p=ptr(10);
xyz(ptr(20));
funptr(ptr(30));
a=*p;
ptrinf(“\n static data in main : %d”,a);
static data in main 88.

35



  • Where ever the function is not returning only values then specify the return type as void.
  • void means nothing i.e, return value

void abc()
{
printf(“Hello abc”);
}
void main()
{
abc();
}
o/p : Hello abc

  • If the function return type is void then it is possible to place empty return statement.
  • From void() function when we are placing empty return statement then control will pass to back to the calling

36

void tex(long int no)
{
switch(no)
{
case 1 : printf (“one”);
break;
case 2 : printf (“two”);
break;
case 3 : printf (“three”);
break;

case 4 : printf (“four”);
break;
case 5 : printf (“five”);
break;
case 6 : printf (“six”);
break;
case 7 : printf (“seven”);
break;
case 8 : printf (“eight”);
break;
case 9 : printf (“nine”);
break;
case 10 : printf (“ten”);
break;
case 11 : printf (“eleven”);
break;
case 12 : printf (“twelve”);
break;
case 13 : printf (“thirteen”);
break;
case 14 : printf (“fourteen”);
break;
case 15 : printf (“fifteen”);
break;
case 16 : printf (“sixteen”);
break;
case 17 : printf (“seventeen”);
break;
case 18 : printf (“eighteen”);
break;
case 19 : printf (“nineteen”);
break;
case 20 : printf (“twenty”);
break;
case 30 : printf (“thirty”);
break;
case 40 : printf (“forty”);
break;
case 50 : printf (“fifty”);
break;
case 60 : printf (“sixty”);
break;
case 70 : printf (“seventy”);
break;
case 80 : printf (“eighty”);
break;
case 90 : printf (“ninety”);
break;
case 100 : printf (“hundred”);
break;
case 1000 : printf (“Thousand”);
break;
case 1000000 : printf (“Lakh”);
break;
case 10000000 : printf (“Crore”);
break;
}// end of switch

} // end of text.

void digittotx(long int n)
{
long int t;
if(n==0)
printf(“Zero”);
if(n>=10000000)
{
t=n/10000000;
if(t<=20)
text(t)
else
text(10000);
text(t%10);
}

text(10000000);
n=n%10000000;
}
if(n>=1—–)
{
t=n/100000;
if(t<=20)
{
text(t);
else
{
text(t/10*10);
text(t%10);
}
text(100000);
}
if(n>=1000)
{
t=n/1000;
if(t<=20)
text(t);
else
{
text(t/10*10);
text(t%10);
}
text(1000);
n=n%1000;
}
if(n>=100)
{
t=n/100;
text(t);
text(100);
n=n%100;
}
if(n<=20)
text(n);
else
{
text(n/10*10);
text(n%10);
}

void currency (long double rps)
{
long int r,p;
r=(long int)rps;
digittotxt(r);
printf(“Rupees“);
p=(rps-r)*100;
digittotxt(p);
printf(“paise”);
}

void main()
{
long double n;
clrscr();
printf(“Enter amount:”);
scanf(“%LF”,&n);
currency(n);
getch();
 }