Friday, October 29, 2010

structure inside the another structure

/*
 * Structure inside the another structure.c
 *
 *  Created on: Oct 19, 2010
 *      Author: Karthikeyan.D
 */

# include <malloc.h>
struct employee
{
    int nage;
    int nsex;

};

struct NetSim
{
    int nTest;
    struct employee *pstruEmp;
};

struct NetSim *pstruNetsimDepartment =NULL;


# include <stdio.h>
int main()
{
  //To create  a memory for first structure
    pstruNetsimDepartment = (struct NetSim * )malloc (sizeof(struct NetSim *));

  //To create  a memory for second structure
    pstruNetsimDepartment->pstruEmp = (struct employee * )malloc (sizeof(struct employee *));

    // To assign the employee age as ten

    pstruNetsimDepartment->pstruEmp->nage =10;
// echo this meesge
    printf("%d",pstruNetsimDepartment->pstruEmp->nage);
    return 0;
}

Wednesday, October 20, 2010

netsh command usage

Sample:

INPUT:

netsh interface ip show address "Local Area Connection 2"

OUTPUT:

Tuesday, October 19, 2010

Sample make File

myprogram:
g++ stack.c -o stack.exe
g++ revfile.c -o revfile.exe
clean:
rm -rf *.o *.exe

Friday, October 15, 2010

Memory Reference Problem

/*
* StruMem.c
*
* Created on: Oct 15, 2010
* Author: KArthikeyan.D
*
*/
# include <stdio.h>
# include <malloc.h>
# include <string.h>
typedef struct struture1
{
int a;
int b;

}stru;

int main()
{
stru *Memory = (stru*)malloc(sizeof(stru));
//Memory = NULL;

Memory->a=12;
Memory->b = 21;

free(Memory);
printf("%d ",Memory->b);
return 0;
}

Note: View last two lines. Memory freed . After that we are trying to get the value.

OutPut: Garbage value

Sunday, October 10, 2010

How to print the single linked list in reverse order without using array or memory?

/*
* list1.c
* Display the single linked list in reverse order
* Created on: Oct 11, 2010
* Author: Karthikeyan.D
*/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct struNode
{
int nVal;
struct struNode *pstruNext;
}pstruStart;

int fnReversePrint(pstruStart* nTempLoation);

int fnReversePrint(pstruStart* nTempLoation)
{

if(nTempLoation->pstruNext != NULL)
{
fnReversePrint(nTempLoation->pstruNext);
printf("-->%d",nTempLoation->nVal);
}
else
{
printf("NULL-->%d",nTempLoation->nVal);
}

return 0;

}
int main()
{
struct struNode *pstruStart=NULL;
struct struNode *pstruNewnode,*pstruCurrent;

int nN;
int nI;

printf("enter the number of nodes\n");
scanf("%d",&nN);
for(nI=0;nI<nN;nI++)
{
pstruNewnode=(struct struNode*)malloc(sizeof(struct struNode));
printf("enter the data\n");
scanf("%d",&pstruNewnode->nVal);
pstruNewnode->pstruNext=NULL;
if(pstruStart==NULL)
{
pstruStart=pstruNewnode;
pstruCurrent=pstruNewnode;
}
else
{
pstruCurrent->pstruNext=pstruNewnode;
pstruCurrent=pstruNewnode;
}
}
printf("the linked list is\n");
pstruNewnode=pstruStart;
while(pstruNewnode!=NULL)
{
printf("%d----->",pstruNewnode->nVal);
pstruNewnode=pstruNewnode->pstruNext;
}
printf("NULL\n");


fnReversePrint(pstruStart);

return 0;

}

INPUT:

enter the number of nodes: 5
enter the data: 10
enter the data: 20
enter the data: 30
enter the data: 40
enter the data: 50

OUTPUT:
the linked list is
10----->20----->30----->40----->50----->NULL
NULL-->50-->40-->30-->20-->10

Sunday, October 3, 2010

Counting bits

/*
* To count the number bits used for the value representation.
*/

# include <stdio.h>
# include < stdlibb.h>

int bitcount(unsigned char x)
{ int count;
for (count=0; x != 0; x>>=1)
if ( x & 01) count++;
return count;
}
int main()
{
int BitCount;


BitCount = bitcount(10);

printf("Number of BitCount = %d\n ",BitCount);


return 0;
}

Timer Problem

/* timer.c */
/* Computes the time in seconds to do a computation */
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
main()
{ int i;
time_t t1,t2;
(void) time(&t1);
for (i=1;i<=1000;++i)
printf(``%d %d %dn'',i, i*i, i*i*i);
(void) time(&t2);
printf(``n Time to do 1000 squares and
cubes= %d secondsn'', (int) t2-t1);
}