-
Need HELP QUICK
im having some problems with my code. after the code is a list of the errors i got. could someone please help. The lines that im having trouble with are:
if (a[].xx == b[].xx && a[].yy == b[].yy) <- in local_address
l = local_address(a[j], a[k]); <- in main
Code:
/***************************************************************\
*
* This program processes a list of internet addresses,
* identifying all pairs of computers from the same locality
* Each IP address is inputted with an associated nickname.
* eg (192.753.0.4 green).
*
* Up to 100 addresses and nicknames can be inputted, the
* nickname can be up to 10 characters in length, the list
* can be terminated by a sentinel address and sentinel
* nickname (0.0.0.0 none).
*
* The program displays a list of messages identifying each
* pair of computers form the same locality, that is, each
* pair o computers with matching values in the first two
* components of the address. Following the messages is the
* full list of addresses and nicknames.
*
*
* BY ANDREW WILKINSON (13380698)
*
\***************************************************************/
#include <stdio.h>
#include <stdlib.h> /* Declares exit() */
#define NUM_ADDRESSES 100 /* Max size of the number of IP address's */
#define NUM_CHARACTERS 11 /* Max number of characters for the nickname */
/* Defining a structure type called address_t that has 4 members for the IP
address values, and 1 member for the computers nickname. */
struct address_t {
int xx;
int yy;
int zz;
int mm;
char NickName[NUM_CHARACTERS];
};
void scan_address(struct address_t *ap[])
{
/* Declaring variable i for changing the element of the array */
int i = 0;
printf("Please enter the list IP address's followed by there nickname's (xx.yy.zz.mm nickname)\n");
printf("To finish entering the list, please insert a sentinel address and nickname (0.0.0.0 none):\n");
/* Reading in a list of up to 100 addresses and there nicknames, and being terminated
by a sentinel address of all zeros and a sentinel nickname of none. */
do {
/* Scans in the IP address and nickname, and saves them in the structure */
scanf("%d.%d.%d.%d %s", &ap[i]->xx, &ap[i]->yy, &ap[i]->zz, &ap[i]->mm, &ap[i]->NickName);
/* A value of an IP address is not greater then 250 */
if (ap[i]->xx > ap[i]->yy > ap[i]->zz > ap[i]->mm > 250) {
printf("ERROR: A value of an IP address is not greater then 250.\n");
exit(-1); /* Exits program if above is true */
}
/* The nickname can't be longer then 10 characters */
else if ((int) sizeof ap[i]->NickName > 11) {
printf("ERROR: The computers nickname can't be longer then 10 characters.\n");
exit(-1); /* Exits program if above is true */
}
else
i++;
} while ((ap[i]->xx != 0 && ap[i]->yy != 0 && ap[i]->zz != 0 && ap[i]->mm != 0
&& ap[i]->NickName != "none") || i <= NUM_ADDRESSES);
}
int local_address(struct address_t a[], struct address_t b[])
{
/* Declaring value to return. */
int value;
/* Checks to see if the values in the first two components of
the address are the same, if so return 1, if not, return 0 */
if (a[].xx == b[].xx && a[].yy == b[].yy)
value = 1;
else
value = 0;
return value;
}
void print_address(struct address_t a[])
{
/* Declaring variable h for changing the element of the array */
int h;
/* Prints out the list of addresses and nicknames */
for (h = 0; h < NUM_ADDRESSES; h++)
printf("%d.%d.%d.%d %s\n", a[h].xx, a[h].yy, a[h].zz, a[h].mm, a[h].NickName);
}
int main(void)
{
/* Declaring an array, a, of type address_t */
struct address_t a[NUM_ADDRESSES];
/* Declaring variable j and k for changing the element of the array */
/* Declaring variable l for the return value of the function local_address */
int j, k, l;
/* Calling a function that scans in the data, and saves it in structure a. */
scan_address(&a);
/* Creating a loop to input values into the function local_address
so they can be tested to see if they are from the same network. */
for (j = 0; j < NUM_ADDRESSES; j++) {
for (k = 0; k < NUM_ADDRESSES; k++) {
if (j < k) {
/* Calling a function that takes two address structures
and checks if they are on the same local network. If
they are then it returns 1, if not returns 0. */
l = local_address(a[j], a[k]);
/* If addresses are from the same network, then print message. */
if (l == 1)
printf("Machines %s and %s are on the same local network.\n", a[j].NickName, a[k].NickName);
}
}
}
/* Calling a function that prints out the names of the computers
that are on the same local network, and then prints out all
the list of addresses and nicknames. */
print_address(a);
return 0;
ERRORS
Wedit output window build: Fri Nov 3 02:39:10 2006
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 illegal expression
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 left operand of . has incompatible type 'int'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 illegal expression
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 left operand of . has incompatible type 'int'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 syntax error; found `)' expecting ']'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 operands of == have illegal types 'int' and 'struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 syntax error; found `)' expecting ']'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 illegal expression
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 left operand of . has incompatible type 'int'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 illegal expression
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 left operand of . has incompatible type 'int'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 syntax error; found `)' expecting ']'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 operands of == have illegal types 'int' and 'struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 syntax error; found `)' expecting ']'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 operands of && have illegal types 'struct address_t' and 'struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 operands of != have illegal types 'struct address_t' and 'int'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 79 operands of != have illegal types 'struct address_t' and 'int'
Warning "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 106 assignment of pointer to array 100 of struct address_t to pointer to pointer to struct address_t
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 116 type error in argument 1 to `local_address'; found 'struct address_t' expected 'pointer to struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment.c": 116 type error in argument 2 to `local_address'; found 'struct address_t' expected 'pointer to struct address_t'
Compilation + link time:0.2 sec, Return code: 1
-
More than likely, local_address doesn't need to take arrays for parameters. Take all the square brackets out of that function and see if that's what you were expecting.
-
You have lots of issues with pointers here.
First, in main, you have this array:
Code:
struct address_t a[NUM_ADDRESSES];
You want to pass a pointer to the first element of the array to scan_address.
But it should really be like this.
And then you need to change your function's signature from this
Code:
void scan_address(struct address_t *ap[])
to this
Code:
void scan_address(struct address_t *ap)
This in turn means that you need to change this
Code:
do {
/* Scans in the IP address and nickname, and saves them in the structure */
scanf("%d.%d.%d.%d %s", &ap[i]->xx, &ap[i]->yy, &ap[i]->zz, &ap[i]->mm, &ap[i]->NickName);
/* A value of an IP address is not greater then 250 */
if (ap[i]->xx > ap[i]->yy > ap[i]->zz > ap[i]->mm > 250) {
printf("ERROR: A value of an IP address is not greater then 250.\n");
exit(-1); /* Exits program if above is true */
}
/* The nickname can't be longer then 10 characters */
else if ((int) sizeof ap[i]->NickName > 11) {
printf("ERROR: The computers nickname can't be longer then 10 characters.\n");
exit(-1); /* Exits program if above is true */
}
else
i++;
} while ((ap[i]->xx != 0 && ap[i]->yy != 0 && ap[i]->zz != 0 && ap[i]->mm != 0
&& ap[i]->NickName != "none") || i <= NUM_ADDRESSES);
to this
Code:
do
{
/* Scans in the IP address and nickname, and saves them in the structure */
scanf("%d.%d.%d.%d %s", &ap[i].xx, &ap[i].yy, &ap[i].zz, &ap[i].mm, &ap[i].NickName);
/* A value of an IP address is not greater then 250 */
if ( ap[i].xx > ap[i].yy > ap[i].zz > ap[i].mm > 250 )
{
printf("ERROR: A value of an IP address is not greater then 250.\n");
exit(-1); /* Exits program if above is true */
}
/* The nickname can't be longer then 10 characters */
else if ( (int) sizeof ap[i].NickName > 11 )
{
printf("ERROR: The computers nickname can't be longer then 10 characters.\n");
exit(-1); /* Exits program if above is true */
}
else
i++;
} while ( (ap[i].xx != 0 && ap[i].yy != 0 && ap[i].z != 0 && ap[i].mm != 0
&& ap[i].NickName != "none") || i <= NUM_ADDRESSES );
And I'd really like to know what you expect empty brackets to mean in your local_address function.
Code:
int local_address(struct address_t a[], struct address_t b[])
{
/* Declaring value to return. */
int value;
/* Checks to see if the values in the first two components of
the address are the same, if so return 1, if not, return 0 */
if (a[].xx == b[].xx && a[].yy == b[].yy)
value = 1;
else
value = 0;
return value;
}
Having chosen that particular form for the function's signature makes it less obvious that a and b are just pointers to struct address_t. So it appears you've succeeded in confusing yourself.
Code:
if ( a->xx == b->xx && a->yy == b->yy )
Now, I think that's all that was needed for a clean compile.
But of course there are several logic errors here, so I'll let you think about it a little.
Code:
} while ((ap[i]->xx != 0 && ap[i]->yy != 0 && ap[i]->zz != 0 && ap[i]->mm != 0
&& ap[i]->NickName != "none") || i <= NUM_ADDRESSES);
If fact, I think I'd choose a for loop with a break in an if input validation.
Then after that you'll need to keep track of the actual number of entries and use that in your print loop. You may also find it advantageous to initialize your array in main.
-
Thankyou for your help with the pointers, that cleared allot of my errors up, but im still not shore about logical errors. is there somone that can help
Code:
if (a->.xx == b->.xx && a->.yy == b->.yy)
Code:
l = local_address(a[j], a[k]);
Ther errors are:
Wedit output window build: Fri Nov 3 09:49:37 2006
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 field name expected
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 left operand of . has incompatible type 'pointer to struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 field name expected
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 left operand of . has incompatible type 'pointer to struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 field name expected
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 left operand of . has incompatible type 'pointer to struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 field name expected
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 79 left operand of . has incompatible type 'pointer to struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 116 type error in argument 1 to `local_address'; found 'struct address_t' expected 'pointer to
+ struct address_t'
Error "c:\documents and settings\user\my documents\wilko\lcc\assignment2.c": 116 type error in argument 2 to `local_address'; found 'struct address_t' expected 'pointer to
+ struct address_t'
Compilation + link time:0.3 sec, Return code: 1
-
Did I write this?
Code:
if (a->.xx == b->.xx && a->.yy == b->.yy)
-
bzboard if you are having some problems to grok pointers I would suggest you reading this -> http://pw1.netcom.com/~tjensen/ptr/pointers.htm it is fast to read and will clear most of your doubts.
-