C++ reference FAQ interview questions

//1.References are nothing but constant pointer.
//2.So,references once initialized can not refer to other.
//2.1 Reference to reference not allowed.No error but. it will start pointing to the first variable
//3. reference arithmatic not allowed.No error in reference arithmatic but changes reflected in referant not in reference.
//4.Reference to array allowed.//int (&r)[5];
//5.Array of reference not allowed because we can not find the address of array. ultimately refer to first variable.
//6.Unlike pointers,refernce are automatically gets dereferenced.No need to use *(value at address).
//7.Unlike pointers, reference arithmatic like increment will increase the referant's value and not the address
//8.Reference are used to pass by reference to get the changes reflected in calling function.
//9.Pass by reference avoids copy of large value when structure like data are passed as arguments.
//10. Its unsafe to return local variable by value because variable goes out of scope once conrol returns out of the function defintion.
//Solution is us local static variable or dynamic memory allocation so that scope of the variable will through out the program.
#include<iostream>
using namespace std;
int gv;
int main()
{
//reference nothing but constant pointer means once initialized can not be reinitialized.
int s=10;
int *const h=&s;//s is const pointer so can not hold another address
//h=&m;//error: assignment of read-only variable ‘h’


int i=10,m=2;
int &j=i;// j is reference to a

//reference to refernce not allowed. No error but points to the first variable
int &k=j;//reference to reference no error but//point 2.1
k=20;
cout<<i<<endl;//20

//pointer arithmatic are not allowed. no error but changes gets reflected in first variable
k++;//point 7
cout<<i<<endl;//21

//referece can not be reinitialized but direct values can be given
int a=1;
k=1;
cout<<k<<endl;

//pass by reference to get the values reflected in calling function
int x=10,y=20;
swap(x,y);
cout<<x<<" " <<y<<endl;//20,10

//if return type is reference then function call can be made in left side
int &f();
f()=5;
cout<<"gv="<<gv<<endl;//5//instead of returned reference 100, f() is assigned to 5

//problem of local ovariable returning by value
int d;
int ff(d);
cout<<"d="<<d<<endl;//Garbage, because return value went out of scope in definition itself.
return 0;
}
void swap(int x, int y)
{
int t;
t=x;
x=y;
y=x;
}
//return by reference
int &f()
{
gv=100;
return gv;
}
//problem of returning local variable by value
int ff(int t)
{
int& lv=t;
lv=lv+44;
return lv;//returning local variable by value.
}//scope of lv over here
You may also like.

Find and remove loop in linked list code in cpp linux

//Remove Loop in a linked list1.
//Find the loop and pass the loop node to removeLoop function.
//assign pointer ptr1=start.
//start infinite loop.
//assign pointer ptr2=loopnode.
//in loop traverse till ptr2->next!=loopnode && ptr2->next!=ptr1
//loop is found when ptr2->next=ptr1 ; break the loop
//else increment ptr1//most important
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
//Inserting at start
void insert(struct node** ref,int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*ref);
*ref=newnode;//mind it
}
//displaying
void display(struct node *ref)
{
struct node *ptr=ref;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
//Detecting Loop
void loopDetect()
{
struct node* slow=start;
struct node* fast=start;
while(start->next !=NULL)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
printf("LoopDetected\n");
removeLoop(slow);
break;//else infinite time LoopDetected gets printed
}
}
}
void removeLoop(struct node *loopnode)
{
printf("\nRemoveLoop Called.\n");
struct node *ptr1;
struct node *ptr2;
ptr1=start;
while(1)
{
ptr2=loopnode;
while(ptr2->next != loopnode && ptr2->next!=ptr1)
{
ptr2=ptr2->next;
}
if(ptr2->next==ptr1)//mind the next
break;
else
ptr1=ptr1->next;
}
ptr2->next=NULL;
}
int main()
{
insert(&start,10);
insert(&start,20);
insert(&start,30);
insert(&start,40);
insert(&start,50);
display(start);
//create a loop
start->next->next->next->next=start->next;
//display(start);//infinite loop means loop created.
loopDetect();
display(start);//No infinite loop means No loop now.

return 0;
}
/*
50 40 30 20 10
LoopDetected
50 40 30 20
*/
--------------------------
//Remove Loop in a linked list1.

C Linux code to insert in middle of Linked list

//Inserting After given node of Single Linked List
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
        int data;
        struct node* next;
};
struct node *start=NULL;
void push(struct node** ref,int d)
{
        struct node *newnode=(struct node *)malloc(sizeof(struct node));
        newnode->data=d;
        newnode->next=(*ref);
        (*ref)=newnode;
}
void display(struct node *start)
{
        struct node *ptr=start;
        while(ptr)
        {
                printf("%d\t",ptr->data);
                ptr=ptr->next;
        }
        printf("\n");
}
struct node* insertAfterNode(struct node *start,int d, int afternode)
{
int i;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
struct node* current=start;
while(current!=NULL)
{
if(current->data==afternode)
{
newnode->next=current->next;
current->next=newnode;
//return start;
}
current=current->next;
}
}
int main()