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:
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.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 ;
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.
Please suggest how I can implement this requirement.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 } }