Thread: Handling Structure Pointers in Array

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Handling Structure Pointers in Array

    Hi All

    I am creating program which will initialized structures for various record type and will call for N tables.

    The structure will have info regarding the tables.

    Since the init() function will called N times for N tables, so N structures will be created and I want them to store them in Array of structures.

    The code is:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <stddef.h>
    
    char *colArr[]={"IDNAME","IDAGE"};
    char *colType[]={"CHAR","INT"};
    
    
    
    typedef struct
    {
    	char colName[10];
    	char colType[10];
    	char colLen[10];
    } colInfo;
    
    typedef struct
    {
    	char tableName[10];
    	int colCount;
    	colInfo *pCol;
    } tableInfo;
    
    tableInfo *tPtr[] = {0x00};
    int fileCount = 0;
    
    int init()
    {
       tableInfo tInfo;
       
       int i; 
       memset(&tInfo,0x00,sizeof(tInfo));
    
       tInfo.pCol = (tableInfo *)malloc(sizeof(tableInfo) * 2);
    
       memcpy(tInfo.tableName,"IDTABLE",7);
       tInfo.colCount = 2;
    
       for (i = 0; i< tInfo.colCount ;i++)
       {
    	   memcpy(tInfo.pCol[i].colName,colArr[i],6);
    	   memcpy(tInfo.pCol[i].colType,colType[i],4);
    	   memcpy(tInfo.pCol[i].colLen,"4",1);
       }
    
       printf("%s\n",tInfo.tableName);
       printf("%d\n",tInfo.colCount);
       for (i = 0; i < tInfo.colCount;i++)
       {
    	   printf("%s\n",tInfo.pCol[i].colName);
    	   printf("%s\n",tInfo.pCol[i].colType);
    	   printf("%s\n",tInfo.pCol[i].colLen); 
       }
    
      // Here I need info  
        tPtr[fileCount] =&tInfo;
         fileCount++;
    	 
       return 1;
    }
    tPtr[fileCount] =&tInfo;

    The above is not working as when I memset, every thing vanish since second time call to memset delete everything.
    Can any body help me in this regard so that I can store structures in array and values should not vanish.


    Thanks
    Nickman
    Last edited by nickman; 11-19-2011 at 03:43 AM. Reason: change

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Malloc() is in stdlib.h. I'm not sure what stddef.h has in it. Your cast of the return from malloc, will (rather oddly), conceal the error you may receive otherwise, about this.

    At the least, I'd check the return from these called functions to ensure that they actually worked.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Hi

    The return values are working..

    My concern is to save the values in arrays in a way that I can use them again.


    NOTE: This code is called in single flow.Also please ignore libs and other points like casting,return values,free memory etc.
    This is not complete code,can consider as pseduo.
    Last edited by nickman; 11-19-2011 at 02:09 PM. Reason: update

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in handling double pointers!!
    By salmanriaz in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2009, 11:28 AM
  2. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  3. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  4. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  5. Window priority handling for a GUI - data structure
    By rmullen3 in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2003, 07:06 PM