Solved

# Compare structure componets in a function

Posted on 1998-05-21
283 Views
I have a function to compare individual structure componets but it will not compare them. See Function below.
int
{
int i;
int status;

for (i = 0; i < x; ++i)
{
2_array[i++] = v;
status = 1;
}
else
{
status = 0;
}
return(status);

0
Question by:moorem
• 4
• 3

LVL 10

Expert Comment

ID: 1250891
What is this function trying to do ???
0

LVL 10

Accepted Solution

RONSLOW earned 100 total points
ID: 1250892

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

{
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 !!

0

Author Comment

ID: 1250893
Hi Ronslow

I have a structure like below.
typedef
{
char name[50];
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.
0

LVL 10

Expert Comment

ID: 1250894
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 i;
for (i = 0; i < 2; i++) {
}
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 i;
/* check all the currently stored addresses */
for (i = 0; i < *pCount; i++) {
/* if already there then return 0 */
return 0;
}
}
/* wasn't there .. add to the end */
/* no check on array overflow though */
/* bump up the count becuase we just add a new address */
(*pCount)++;
return 1;
}

Is this what you are trying to do??

0

Author Comment

ID: 1250895
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.
0

LVL 10

Expert Comment

ID: 1250896
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 i;
/* check all the currently stored addresses */
for (i = 0; i < count; i++) {
/* if already there then return 0 */
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.

0

Author Comment

ID: 1250897
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.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.