Saturday, December 15, 2012

Example for Dot and arrow usage of structural pointer and array.

# include <stdio.h>
# include <stdlib.h>
# include <string.h>


typedef struct database
{
    int age;
    char name[10];

}DATA;

DATA Str;
DATA *Ptr;




int main()
 {


    /*********** Dot format to Arrow format representation********/

     //Enter the values for database
    printf("1. Enter the values for database\n");
    printf("Enter your Age\n");
    scanf("%d",&(Str.age));
    scanf("%s",Str.name);

    printf("Your Age:%d is updated in database\n",(&Str)->age);
    printf("Your Name:%s is updated in database\n",(&Str)->name);
    /**************************************************************/



    Ptr =(DATA *)malloc(sizeof(DATA));
    /*********** Arrow format to Dot format representation *********/

        printf("2. Enter the values for database\n");
        printf("Enter your Age\n");
        scanf("%d",&(Ptr)->age);

        scanf("%s",Ptr->name);

        printf("Your Age:%d is updated in database\n",((*Ptr).age));
        printf("Your Name:%s is updated in database\n",(*Ptr).name);
    /**************************************************************/

     return 0;
 }


Output:


F:\workspace\pointer_C\Debug>pointer_C.c.exe
1. Enter the values for database
Enter your Age
24
kumar
Your Age:24 is updated in database
Your Name:kumar is updated in database
2. Enter the values for database
Enter your Age
23
Ramesh
Your Age:23 is updated in database
Your Name:Ramesh is updated in database

F:\workspace\pointer_C\Debug>

Difference between Dot(.) and Arrow (*) in C language


Difference between Dot(.) and Arrow (*) in C language
 






 
Representation 
Dot (.)
Arrow (*)
The Dot (.) operator can't be overloaded.
 Arrow (->) operator can be overloaded.
ex:(*foo).bar()
  ex:foo->bar()
 






 
Note1: The parenthesizes above are necessary because of the binding strength of the * and . operators.
Note2: *foo.bar() wouldn't work because Dot (.) operator binds stronger and is executed first. 
 






 
Possible way of dot and arrow usage for non pointer  element in C.
 
 






 
ptr->fld == (*ptr).fld





 
str.fld == (&str)->fld              

Friday, December 14, 2012

How to read element in Structural Array and pointer in C

/*
 * pointer.c
 *
 *  Created on: Dec 15, 2012
 *      Author: Karthik
 */

# include <stdio.h>
# include <stdlib.h>

struct database
{
    int nstArray[10];
    int *nstpointer;
}STDBase;


int main()
{
    // Initialization
    int nArray[10];
    char cArray[10];
    int *nPointer;
    int nLoop;

    //Memory allocation
    nPointer = calloc(1,10);

    // Initialization for int, char arrays and int pointer
    for(nLoop = 0; nLoop < 10; nLoop++)
    {
        // To int array
        nArray[nLoop] = nLoop;
        printf("%d\t",nArray[nLoop]);

        // To char array
        cArray[nLoop] = nLoop+48;
        printf("%c\t",cArray[nLoop]);

        // To int pointer
        *nPointer    = nLoop+2;
        printf("%d\n",*nPointer);
        nPointer++;

    }
   nLoop--;
    STDBase.nstArray[nLoop] =10;
    STDBase.nstpointer = (STDBase.nstArray);

    printf("%d\n",STDBase.nstArray[nLoop]);
    printf("%d\n",STDBase.nstpointer[nLoop]);


    return 0;
}


Output:
 0    0    2
1    1    3
2    2    4
3    3    5
4    4    6
5    5    7
6    6    8
7    7    9
8    8    10
9    9    11
10
10





Saturday, October 13, 2012

Difference Between Build and Compilation in C/C++


Build is the process to compile and link only the source files that have changed since the last build (i.e. based on file creation date and time).

Rebuild is the process to compile and link all source files regardless of whether they changed or not. Build is the normal thing to do and is faster. Sometimes the versions of project target components can get out of sync and rebuild is necessary to make the build successful.

Compilation is compiles the source file currently being edited.

The compilation of code involves several steps:

  • Parsing of the statements for syntax (language based)
  • Translation of the statements into machine language
  • Setting up the addresses of all variables
  • Optimization of the code (if desired)

Thursday, October 4, 2012

Bitwise Operators and Byte to Bit Conversion

# include "stdio.h"
# include "string.h"

const char* byte_to_binary( int x );
int main()
{

    int nInput=0, nOutput=2, nBitPos;

    // To set the bit in particular position.
    nInput |= (1 << nOutput);
    printf("To set the %d Position from 1bit of %s\n",nOutput, byte_to_binary(nInput));

    // To Reset/Clear the bit in particular position.
    nInput &= ~(1 << nOutput);
    printf("To Reset/Clear the %d Position from 1bit of %s\n",nOutput, byte_to_binary(nInput));

    // To toggle the bit in particular position.
    nInput =5; // 00000101 expected like 00000001
    nInput ^= (1 << nOutput);
    printf("To toggle the %d Position from 1bit of %s\n",nOutput, byte_to_binary(nInput));

    // To state of the bit in particular position.
    nInput =5; // 00000101 expected like 00000101
    nBitPos = nInput &(1 << nOutput);
    printf("To state of the %d Position from 1bit of %s:: state of bit: %d\n",nOutput, byte_to_binary(nInput), nBitPos);


return 0;
}



//To Byte to Binary Conversion
const char* byte_to_binary( int x )
{
    static char b[9] = {0};
    int z, y;

    for (z=128,y=0; z>0; z>>=1,y++)
    {
        b[y] = ( ((x & z) == z) ? '1' : '0');
    }

    b[y] = 0;

    return b;
}



Output:
To set the 2 Position from 1bit of 00000100
To Reset/Clear the 2 Position from 1bit of 00000000
To toggle the 2 Position from 1bit of 00000001
To state of the 2 Position from 1bit of 00000101:: state of bit: 4

Tuesday, August 21, 2012

Several CMD Commands in parallel Execution


start cmd.exe /c "C:\Program Files\Mozilla Firefox\firefox.exe"
exit
start cmd.exe /c "C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE"
exit

1. make these commands as *.bat file.

2. Keep this new *.bat file in "C:\Documents and Settings\All Users\Start Menu\Programs\Startup" location.

3. While login windows OS, it will invoke the above *.exe automatically.

Advantage:
** It will reduce the number of user interaction for every login.

Friday, August 3, 2012

QuickSort function Sample Code

//Program to sort names in an array using quicksort
#include <stdio.h
>
#include<conio.h>
#include <stdlib.h>
#include <string.h>

int sort_function( const void *a, const void *b);

char list[5][4] = { "cat", "car", "cab", "cap", "can" };

int main(void)
{
int x;
clrscr();
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x
< 5; x++)
printf("%s ", list[x]);
getch();
return 0;
}

int sort_function(const void *a,const void *b)
{
return( strcmp((char *)a,(char *)b) );
}

Wednesday, July 18, 2012

Logic Thinking

#include <stdio.h>
#include <conio.h>

int main()
{
#define HAPPY (1)
#define SATISFIED (1)
if(!HAPPY != SATISFIED)
{
printf("Life");
}
else
{
printf("Heaven");
}

getch();
return 0;
}

Output:



Its in Your Hand


Ans: Life

Wednesday, June 20, 2012

C PreProcessor and Intermediate C Code

# include <stdio.h>
# include <windows.h>

# define MAX 10


int main()
{
 static int nVariabe; // To understand the static variable constant
 int nloop=0;

    for (;nloop<MAX;nloop++)
    {
        nVariabe++;
        printf("%d static variable %d\n",nloop,nVariabe);

    }

 return 0;

}

 Please Check the For Condition .
From this code you will get the intermediate code information and understanding the C Macros.

Output for intermediate C code Generation




Static Variable Declaration without Data Type

# include <stdio.h>
# include <windows.h>

# define MAX 10


int main()
{
 static nVariabe; // To understand the static variable constant
 int nloop=0;

    for (;nloop<MAX;nloop++)
    {
        nVariabe++;
        printf("%d static variable %d\n",nloop,nVariabe);

    }

 return 0;

}

OUTPUT:

Here, everybody feels like compilation will abort. But, actual result is below,.
0 static variable 1
1 static variable 2
2 static variable 3
3 static variable 4
4 static variable 5
5 static variable 6
6 static variable 7
7 static variable 8
8 static variable 9
9 static variable 10

Note: We will get only Warning....

Description    Resource    Path    Location    Type
type defaults to `int' in declaration of `nVariabe'    Code.c    /intermediateCode    line 17    C/C++ Problem

Saturday, March 24, 2012

User File Browser in VBA

Sub User_FileBrowser()

Dim varFileName As Variant
Dim nLoop As Integer

'For single file selction
'varFileName = Application.GetOpenFilename(, , "Please select source workbook:")

'For Multiple file selection
varFileName = Application.GetOpenFilename(FileFilter:="Excel files (*.xls), *.xls", Title:="Please select source workbook:", MultiSelect:=True)
nLoop = 1


Do

If TypeName(varFileName(nLoop)) = "String" Then
Cells(2, 2).Value = varFileName(nLoop)
Else

MsgBox "Please Select the xls file", vbOKOnly, "Warning!"
Exit Sub

End If

nLoop = nLoop + 1
Loop Until nLoop = UBound(varFileName)

End Sub

How to create message Box2 in VBA

Sub Msg_Box()


MsgBox "Testbox", vbYesNo, "karthiken07"

OutPut
End Sub

How to create message Box in VBA

1.Create Command button using form tool bar.

Sub Msg_Box()


MsgBox "Testbox"

End Sub

Output :