20130428

CPP program for concatenating two strings without using inbuilt library function strcat


How to concatenate two strings using pointer or array. CPP program for concatenating two strings without using inbuilt library function strcat. How to concatenate two strings in cpp/C++/C without using inbuilt in function strcat.Program to concatenate two strings using pointer.

#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

20130426

How to search a substring in C++/Cpp without using library function strstr


/*How to search a sub string without using library function strstr. 
How to use pointer/array to search substring in a string sentence.
C++/CPP/C program/code to search a word in a sentence without using inbuilt function strstr*/
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[50];
printf("Enter a String:\n");
scanf("%s",&arr1);

char arr2[20];
printf("Enter a String:\n");
scanf("%s",&arr2);
printf("Searching for:%s\n",arr2);

20130425

How to use make file in CPP C+++ C

/*What is makefile  ?
---------------------------------------
makefile is a script/utility  that contains target, dependencies and actions to compile modified file/files in a project of multple files.  */

/*SYNTAX
-----------------------------------
target:dependencies
[tab]  actions
.
.

20130422

How to implement msgq message queue in CPP C C++


//Program to implement msgq
/*CPP Program for msg queue in Linux
pipes work with related process parent-child only.
Fifo can make unrelated process to communicate but
fifo created does not persists for use in future.
Shared memory is similar to fifo but over comes this and can be used later also.
Program to implement IPC Message Queues IPC mechanism.
msgqSendkirk.c adds the message on the message queue.
msgqRecvSpock.c reads and removes the message from the message
queue.To use this program first compile and run msgqSendkirk.c
to add a message to the message queue. To see the
Message Queue in other terminal run msgqRecvSpock.c
ipcs -q shows the msg ques.
ipcrm -q 131073 removes the msgq created say 131071 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
//1. declaring the msg structure
struct my_msgbuf
{

20130421

How to use callback function pointer in cpp/C/C++

Program for callback /function pointer in CPP/C/C++

What is function pointers?As we know a pointer is a special variable that folds the address of another variable.
Function pointer is also  a pointer that hold the address of a function.
Int array[10]; // here array is a constant pointer to a 10 element array. That is constant pointer to first element of array.
Can we declare a non-constant pointer to a function? Yes we can .
Int (*funcPtr)();//funcPtr is a pointer to a function that takes no arguments and returns an integers.//parenthesis is must here as we are not assigning the return type.
Int (*funcPtr)(int,int);//funcPtr is a pointer to a function that takes two arguments and returns an integers.

20130413

How to use malloc for 2d array dynamic memory allocation


/*How to use malloc for 2d array dynamic memory allocation. How to write 2-d array with dynamic memory allocation */
#include<stdio.h>
#include<malloc.h>
int main()
{
int r,c,i,j;
printf("Enter No of rows and columns:");
scanf("%d%d",&r,&c);

int **array;
array=(int**)malloc(r*sizeof(int));

for(i=0;i<r;i++)
{
array[i]=malloc(c*sizeof(int));
}

How to use new to allocate memory for 3d array in CPP C++


/*How to use new to allocate memory for 3d array. How to use malloc for 3 dimensional array. CPP program for dynamically allocating memory for 3d array. How to write three dimensional array with malloc with new */

#include<iostream>
using namespace std;
int main()
{
// 3 Dimensions Array
// two 3x4 array
int x = 2, y = 3, z = 4;

int i, j, k;
// memory allocation for 3D

int ***array3D = new int**[x];
for(i = 0; i < x; i++)
{
array3D[i] = new int*[y];
for(j = 0; j < y; j++)
{
array3D[i][j] = new int[z];
}
}

20130411

How to learn stack and flow of function call and control in cpp

/*How to learn stack. Stack is memory in which Last in first out is followed.
When function call is encountered the address of the next statement is stored or pushed in the stack. When the control returns back from the definition to calling function the address is poped out which is nothing but the statement next to the function call and executed.*/
#include<stdio.h>
#include<iostream>
using namespace std;
void function1();
void function2();

int main()
{
printf("1\n");
printf("2\n");
function1();
printf("3\n");
printf("4\n");

return 0;
}
void function1()
{
printf("1.1\n");
printf("1.2\n");
function2();
printf("1.3\n");
printf("1.4\n");
}
void function2()
{
printf("2.1\n");
printf("2.2\n");
}

How to learn recursive function call in cpp/C++/C

/*How to learn recursive function call in cpp/C++/C
 Cpp Program to learn recursive function */
#include<iostream>
using namespace std;
int sum(int m, int k);
int main()
{
int i,m,k;
m=3;
k=2;
i=sum(m,k);
cout<<"sum="<<i<<endl;
//return 0;
}
int sum(int rm, int rk)
{
cout<<rm<<" "<<rk<<endl;
if(rm==0) return(rk);//termination condition
else return(1+sum(rk, rm-1));//recursive condition//if not +1 then sum=0
}

How to distinguish between call by value, call by reference, call by address

/*How to distinguish between call by value, call by reference, call by address.
Program to distinguish between pass by value,pass by reference,pass by address. Call by reference and call by address affects the calling value while call by value doe not. Most important difference is that in call by value new object is created which is some times such as when size of the variable is too large is not desirable so call by reference is preferred over call by value*/

#include<stdio.h>
#include<iostream>
using namespace std;
void callbyreference(int * x);//pass by address
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyvalue(&x);
printf("\nAfter x=%d",x);//15
printf("\n");
return 0;
}

void callbyreference(int * x)
{
*x=*x+5;//mind the*
}


//CALL BY REFERNCE, PASS BY REFERNCE
/*