Showing posts with label read element. Show all posts
Showing posts with label read element. Show all posts

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