Generic Pointers to structures

This is a discussion on Generic Pointers to structures within the C Programming forums, part of the General Programming Boards category; I have a problem in handling pointers to structures: I have an XML as below: <ROOTNODE> <NAME> <ADDRESS> <PHONE> <EMAIL> ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Generic Pointers to structures

    I have a problem in handling pointers to structures:

    I have an XML as below:
    <ROOTNODE>
    <NAME>
    <ADDRESS>
    <PHONE>
    <EMAIL>
    <DATE>

    <SubBranch>
    <NAME>
    <ADDRESS>
    <PHONE>
    <EMAIL>
    <DATE>
    </SubBranch>

    </ROOTNODE>


    I am parsing the XML and populating it into a structure.The structure is:
    Code:
    typedef struct 
    {
    	char Name[20];
    	char Address[20];
    	char Phone[20];
    	char Email[20];
    	SUBBRANCH     stBranch[10];    
    }MAINBRANCH;
    
    typedef struct 
    {
    	char Name[20];
    	char Address[20];
    	char Phone[20];
    	char Email[20];
    }SUBBRANCH ;
    Since the data inside the <ROOTNODE> and that inside the <SubBranch> is repeated, I want to write a single functions which will parse the data and put it into the corresponding structure.

    How would I pass a generic pointer to a structure into a function? Below is the generic function I am trying: Here I am passing the pointer to MAINBRANCH structure and in the function I am typecasting it accordingly.
    Code:
    // Passing the pointer to the main structure and an indicator to the function 
    GetDataFromXML(MAINBRANCH *structptr , int i) 
    {
    	void *temp;  //Declaring a void pointer to handle the incoming structure
    	if(i == 0)   // i =0 for root data
    	{	temp = MAINBRANCH;
                   //Parse data and populate into MAINBRANCH
            }
    	else if (i == 1)  // i =1 for branch data
    	{
    		temp = MAINBRANCH.SUBBRANCH;
                   //Parse and populate into SUBBRANCH
    	}
    	
    }
    Please suggest how I can implement this requirement.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    So, if I understand you right then you want to pass a void pointer to GetDataFromXML, with 'i' identifying its type, then act accordingly within the function. Something like this maybe:
    Code:
    //call casts to void to send it into function
    GetDataFromXML((void*)myMainbranchStructPtr, 0);
    
    //definition converts void pointer to correct type
    GetDataFromXML(void* structptr , int i) 
    {
    	if(i == 0)   // i =0 for root data
    	{	MAINBRANCH* ptr = (MAINBRANCH*)structptr;
                   //Parse data and populate into MAINBRANCH
            }
    	else if (i == 1)  // i =1 for branch data
    	{
    		SUBBRANCH* ptr = (SUBBRANCH*)structptr;
                   //Parse and populate into SUBBRANCH
    	}
    	
    }
    Havent tested it, but I think thats more or less right.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    Better to use an enum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    But if the data becomes
    <NAME>
    <ADDRESS>
    <PHONE>
    <FAX>
    <MOBILE>
    <EMAIL>
    <DATE>

    then you've got a lot of code editing to do all of a sudden.

    Perhaps consider a more generic approach which stores everything as a string, in name,value pairs, all arranged in a hierarchical list.

    Keep the document structure out of the code structure.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Just curious..

    Code:
    <ROOTNODE>
    <NAME>
    <ADDRESS>
    <PHONE>
    <EMAIL>
    <DATE>
    
    <SubBranch>
    <NAME>
    <ADDRESS>
    <PHONE>
    <EMAIL>
    <DATE>
    </SubBranch>
    
    </ROOTNODE>
    While this is valid XML, it doesn't look like well-formed XML. If possible, dunxton, you may want rethink how to store this data using XML (if the XML is something you are constructing yourself as well). And you may want to take heed of Salem's comment, though it all depends on your goal.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by neandrake View Post
    While this is valid XML,
    How it could be? It contains a lot of not closed tags.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Thanks mike_g for the help.

    neandrake,
    This is not the actual XML I am working on. I just posted this for it to look simple. The basic idea I needed , was to process similar set of tags that are in different places in the XML FILe, into different structures using a single common function.

    I didnt know how to pass different structure pointers to a single function.


    My document contains more than a hundred tags whose values I need to populate. Going by salem's advice, I would then have to create more than a hundred strings/Name-Value pairs for every XML I have. Is this a good programming practice( However it does make things simple)?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could also just use a pointer inside a single flavour of struct:
    Code:
    typedef struct branch
    {
    	char Name[20];
    	char Address[20];
    	char Phone[20];
    	char Email[20];
            struct branch *ptr;
    }BRANCH;
    
    BRANCH main, sub;
    main.ptr=sub;  sub.ptr=NULL;
    And you can tell the difference by testing the pointer, if(!(ptr) it's a sub. Which would mean you don't have to worry about this.

    Of course, you are then taking up an extra 5-10%, memory wise.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > I would then have to create more than a hundred strings/Name-Value pairs for every XML I have.
    Code:
    struct attr {
      char *name;
      char *value;
      struct attr *nextAttr;
    };
    struct element {
        char *name;
        struct attr *attrs;
        struct element *nextSibling;
        struct element *child;
    };
    Each <element> you come across, you malloc an element.
    Each name=value attribute, you malloc an attr, and append it to the list of attrs for the current element.
    Another <element> on the same level is a sibling of the element (another list)
    A child <element> is ..., well I'm sure you get the idea now.

    You then have some simple functions which say findElementByName if you want to say find the ADDRESS element, then do something with it when you've found it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers in structures
    By esi in forum C Programming
    Replies: 2
    Last Post: 04-13-2007, 12:41 PM
  2. Generic data structures
    By rzcodeman in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2005, 08:58 AM
  3. pointers and structures
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 07-23-2003, 04:45 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21