Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I want to compare array elements of structure.
I had a following structures,

typedef struct
{ 
  char pDKPid[MAX_LG_ID]; 
  char pNomClient[LG_NOM_CLIENT + 1]; 
  char pNumFacture[LG_NUM_FACTURE + 1];
  char pDocId_PDF[LG_DOC_ID_PDF + 1];
  char pDateFacture[LG_DATE_FACTURE + 1];
  char pTypeFac[LG_TYPE_FAC + 1];
 } sBlocRepSRR;

typedef struct
{  
  sBlocRepSRR pBlocRepSRR[MAX_REPONSES + 1];
  char  pCodeError[LG_CODE_ERREUR + 1];
  int   iNbResponses;

} sRepSRR; 


Using above structures I am retrieving documents from DB.
For each document entry should be stored in sBlocRepSRRstructure.

I am getting total number of elements}(iCount), and want to iterate on that.

for(int i = 0; i<icount;i++)>
{//compare 3 fields
  pResponse->pBlocRepSRR[i].pNumFacture
  pResponse->pBlocRepSRR[i].pDateFacture
  pResponse->pBlocRepSRR[i].pTypeFac

//if all above are MATCHED then I want to compare 
  pResponse->pBlocRepSRR[i].pDocId_PDF
// and need maximum value record
}


for example,
I am getting ,
"0659_01","0659_02","0659_3","0659_04","0659_01","0659_02","0659_3","0659_04"
as pNumFacture
"09/09/2004" is pDateFacture for all
"FG" is pTypeFac
and 00020000217 , 00020001428 etc....are pDOCId_PDF

So final result should be the maximum pDOCId_PDF of similar other values.

How to iterate through array elements in for loop without using pointers?
Please help me out.
Thanks in advance.
Posted
Updated 7-May-10 1:56am
v4

First of all, there are several syntax errors in your question.:mad:

...
for(int i = 0; i<icount;>{
...
pResponse->;pBl...


to answer your question, if you have an array text[8] with the length 8 and the content "0659_01", you can iterate through it with the usage of following code:
for(int index = 0; index < 7; index++)
{
   do_something_with(text[index]);
}

this way the function do_something_with(char c)
will be called with {'0','6','5','9','_','0','1'}.
to get the length of a ASCII array call strlen[^]
 
Share this answer
 
Thanks for reply,
syntax errors coz of I am pasting it so HTML syntax is got affected in my code. I corrected it now.

I am doing as,
for(int i = 0; i < iCount; i++)
{
for (int j = 1; j < iCount; j++)
{
if (pResponse->pBlocRepSRR[i].pNumFacture ==
pResponse->pBlocRepSRR [j].pNumFacture)
{
if (pResponse->pBlocRepSRR[i].pDateFacture ==
pResponse->pBlocRepSRR[j].pDateFacture)
{
if (pResponse->pBlocRepSRR[i].pTypeFac==
pResponse->pBlocRepSRR[j].pTypeFac)
{
if (pResponse->pBlocRepSRR[i].pDocId_PDF >
pResponse->pBlocRepSRR[j].pDocId_PDF)
{
}
}
}
}
with respective else part.
I am comparing each element of array to rest of the elements in array.
Is it ok?

Help me out.
Thanks in advance.
 
Share this answer
 
You are doing too much comparisons than needed and you have a lot of nested if statements; try as follow:

C++
for(int i = 0; i < iCount; i++)
{
   sBlocRepSRR& iStructure = pResponse->pBlocRepSRR[i];

   for (int j = i + 1; j < iCount; j++)
   {
      sBlocRepSRR& jStructure = pResponse->pBlocRepSRR[j];

      if (iStructure.pNumFacture != jStructure.pNumFacture)
         continue;

      if (iStructure.pDateFacture != jStructure.pDateFacture)
         continue;

      if (iStructure.pTypeFac != jStructure.pTypeFac)
         continue;

      if (iStructure.pDocId_PDF > jStructure.pDocId_PDF)
      {
         ...
      }
}
 
Share this answer
 
C#
for(int i = 0; i < iCount; i++)
{
   sBlocRepSRR& iStructure = pResponse->pBlocRepSRR[i];
   for (int j = i + 1; j < iCount; j++)
   {
      sBlocRepSRR& jStructure = pResponse->pBlocRepSRR[j];
      if (iStructure.pNumFacture != jStructure.pNumFacture)
         continue;
      if (iStructure.pDateFacture != jStructure.pDateFacture)
         continue;
      if (iStructure.pTypeFac != jStructure.pTypeFac)
         continue;
      if (iStructure.pDocId_PDF > jStructure.pDocId_PDF)
      {
         ...
      }
}

This Code segment looks correct but when i reading the description I think you want to compare an array or some strings. If you wat to do that you have to do the following:
For string arrays:
C#
for(int i = 0; i < iCount; i++)
{
   sBlocRepSRR& iStructure = pResponse->pBlocRepSRR[i];
   for (int j = i + 1; j < iCount; j++)
   {
      sBlocRepSRR& jStructure = pResponse->pBlocRepSRR[j];
      if (0!=strcmp(iStructure.pNumFacture,jStructure.pNumFacture))
         continue;
      if (0!=strcmp(iStructure.pDateFacture, jStructure.pDateFacture))
         continue;
      if (0!=strcmp(iStructure.pTypeFac, jStructure.pTypeFac))
         continue;
      if (0>strcmp(piStructure.pDocId_PDF , jStructure.pDocId_PDF))
      {
         ...
         A value greater than zero indicates that the first character
         that does not match has a greater value in 
         piStructure.pDocId_PDF than in jStructure.pDocId_PDF

      }
}

For binary data you need to use memorycompareint memcmp ( const void * ptr1, const void * ptr2, size_t num ); instead of stringcompare int strcmp ( const char * str1, const char * str2 );.
Reference:
[memcmp]
[stringcmp]
both need to include #include <string.h>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900