• C

Compare structure componets in a function

I have a function to compare individual structure componets but it will not compare them. See Function below.
int
local_address(address_t 1_array[], address_t 2_array[], int x)
{
 address_t v;
 int i;
 int status;

 for (i = 0; i < x; ++i)
 if (1_array->address[0] == v.address[i] &&
           1_array->address[1] == v.address[i])
 {
   2_array[i++] = v;
   status = 1;
 }
else
 {
   status = 0;
 }
 return(status);
 
mooremAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
What is this function trying to do ???
RONSLOWCommented:

  int local_address(address_t 1_array[], address_t 2_array[], int x) {
    address_t v;

you have non initialized v

    int i;
    int status;

    for (i = 0; i < x; ++i)

a style note .. use {} for multi-line loops even if not strictly necessary
      if (1_array->address[0] == v.address[i] && 
          1_array->address[1] == v.address[i])

you seem VERY confused about arrays and how to dereference them .. we need to talk at length about this !!
      {
        2_array[i++] = v;

this use of i++ here seems very odd

        status = 1;
      }
      else
      {
        status = 0;
      }
    return(status);

status gets set to zero or one throughout the loop .. its value will be the last one set.  This doesn't seem to be what you want to do

  }

You seem to be in a lot of trouble here !!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mooremAuthor Commented:
Hi Ronslow

I have a structure like below.
typedef
{
  int address[4];
  char name[50];
}address_t
The data is read in from a file in the input process and then passed to this function. I need it to compare 1st 2 element
of address & if same store in the other array for ouput.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

RONSLOWCommented:
What do the int values (address) in the address_t mean?  It doesn't really, matter - just curious.

SO... you want to match the first 2 items of the address[4] within address_t.

I'd imagine this to be done with..

int CompareAddresses (address_t* pAddress1, address_t* pAddress2) {
  int i;
  for (i = 0; i < 2; i++) {
    if (pAddress1->address[i] < pAddress2->address[i]) return -1;
    if (pAddress1->address[i] > pAddress2->address[i]) return +1;
  }
  return 0;
}

This function returns 0 if the first 2 elements of address[] +1 if greater, -1 if less.

Now for your storage .. I assume you are passing in a single new address and want to append it to the end of the array of addresses if it is not already there...

int AddNewAddress (address_t* pAddress, address_t addresses[], int* pCount) {
  int i;
  /* check all the currently stored addresses */
  for (i = 0; i < *pCount; i++) {
    /* if already there then return 0 */
    if (0 == CompareAddresses(pAddress, &addresses[i])) {
      return 0;
    }
  }
  /* wasn't there .. add to the end */
  /* no check on array overflow though */
  addresses[*pCount] = *pAddress;
  /* bump up the count becuase we just add a new address */
  (*pCount)++;
  /* return 1 to indicate we added a new address */
  return 1;
}

Is this what you are trying to do??

mooremAuthor Commented:
Ronslow

What I need to do is compare to of the structures componets againts all data in the array and if they are the same copy that element into an array and pass it back to main for the display function. I can e-mail you a copy of the source code if you will give me a e-mail address. Thanks in advance for your help.
RONSLOWCommented:
Thw code I suggested compares a component with all the others in the array and add it if not there (which is what you said you originall said/hinted that you wanted to do).

Now .. you say you want to compare it to each items in the array and a match if any .. this is different

int FindAddress (address_t* pAddress, address_t addresses[], int count) {
  int i;
  /* check all the currently stored addresses */
  for (i = 0; i < count; i++) {
    /* if already there then return 0 */
    if (0 == CompareAddresses(pAddress, &addresses[i])) {
      return i;
    }
  }
  return -1;
}

This function return the array subscript where a match for the given item is found, otherwise it returns -1.  THe calling program can check the return value and take the approrpriate action.

mooremAuthor Commented:
I have tried several of the solutions but it still does not seem to be doing what I think it should do. Maybe it is just me. Thanks for the assistance and now I must move on to the next lab I am a student.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.