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