Program Assistance

  • Thread starter duncanblacksmithmath
  • Start date
D

duncanblacksmithmath

Could someone help me try to fix these errors on this code. Thanks!!

Information:
information:
A function to access an element of a list by its index called
access_i(LL, i). It traverses
the list LL and returns the address of the i-th element in LL, if there
is one. Otherwise it
returns null.

A function to access an element of a list by value called access v(LL,
V). This function
traverses the list LL searching for an element with the value V. The
address of the rst such
element, if found, is returned. Otherwise a null pointer is returned.

errors:
program1.c: In function `access_num':
program1.c:71: warning: comparison between pointer and integer
program1.c: In function `access_val':
program1.c:79: warning: passing arg 1 of `strcmp' from incompatible
pointer type

code:
#include <stdio.h>
#include <stdlib.h>
#include "scan.h"

struct Val
{
char type;
int length;
char value[256];
} Values[1000];

struct Obj
{
struct Obj *p_link;
struct Val *p_value;
struct Obj *n_link;
} Objects[1000], *IdList, *NrList, *SpList, *UnList ;

int i = 0, j = 0; /* 0 <= i,j <= 999, objects and vcalues indices */

struct Obj *List (struct Obj *h, struct Obj *t)
{
h->p_link = NULL;
h->n_link = t;
h->p_value = NULL;
t->p_link = h;
t->n_link = NULL;
t->p_value = NULL;
return h;
}

int Append(struct Obj *L, struct Val *item)
{
struct Obj *Temp = L->n_link;
while (Temp->n_link != NULL)
Temp = Temp->n_link;
if ((i <= 999))
{
(Temp->p_link)->n_link = &Objects;
Objects.n_link = Temp;
Objects.p_link = Temp->p_link;
Temp->p_link = &Objects;
Objects.p_value = &Values[j];
i = i+1;
return 1;
}
else return 0;
}

struct Obj *access_i(struct Obj *l, int num_wanted)
{
int atPoint=0;
struct Obj *current=l;
while(atPoint != num_wanted)
{
atPoint++;
if(current->n_link == NULL) return NULL;
current=current->n_link;
}
return current;
}

struct Obj *access_num(struct Obj *l, int value)
{
struct Obj* current=l;
while(current != NULL && current->p_value != value)
current=current->n_link;
return current;
}

struct Obj *access_val(struct Obj *l, char *value)
{
struct Obj* current=l;
while(current != NULL && !strcmp(current->p_value, value))
current=current->n_link;
return current;
}

int PrintLists(struct Obj *list)
{
struct Obj *Temp = list->n_link;
printf("Type\tLength\tValue\n");
while (Temp->n_link != NULL)
{
printf("%c\t%d\t%s\n", Temp->p_value->type,
Temp->p_value->length, Temp->p_value->value);
Temp = Temp->n_link;
}
}

int main (int argc, char *argv[])
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int Done = 0;
IdList = List(&Objects[0], &Objects[1]);
NrList = List(&Objects[2], &Objects[3]);
SpList = List(&Objects[4], &Objects[5]);
UnList = List(&Objects[6], &Objects[7]);
i = 8; j = 0;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
printf("Symbol: Identifier %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'I';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (IdList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'N':
{
/* process integer number */
printf("Symbol: Integer number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'N';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'F':
{
/* process real number */
printf("Symbol: Real number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'F';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'W':
{
printf("White symbol received\n");
break;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
else
printf("Unprintable character
discovered\n");
break;
}
case 'O':
{
printf("Symbol: Separator %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'S';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (SpList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'E':
{
printf("Error condition: %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'E';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (UnList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
}
} /* end while */
printf("List of NAMES\n");
PrintLists(IdList);
printf("List of NUMBERS\n");
PrintLists(NrList);
printf("List of SEPARATORS\n");
PrintLists(SpList);
printf("List of UNKNOWNS\n");
PrintLists(UnList);
return 0;
}
 
I

Ian Collins

errors:
program1.c: In function `access_num':
program1.c:71: warning: comparison between pointer and integer
program1.c: In function `access_val':
program1.c:79: warning: passing arg 1 of `strcmp' from incompatible
pointer type
These are warnings, much the same as last time you posted. Learn to
read them, they tell a story worth reading.

Look at what the compiler is telling you, you are comparing a pointer
with an int. Don't.

You are passing an Obj pointer to strcmp.

You are missing <strings.h> which doesn't help.

Please don't use tabs in your editor if you are going to copy code to a
news group posting.
 
M

Mark McIntyre

On 5 Feb 2006 15:07:02 -0800, in comp.lang.c ,
Could someone help me try to fix these errors on this code. Thanks!!

without you pointing us to the right lines, itrs tricky...
program1.c:71: warning: comparison between pointer and integer

this tells you that at line 71, you're trying to compare integers and
ponters. No matter what you might have been told, they are not the
same thing.

I think its probably this line by the way:
struct Obj* current=l;
while(current != NULL && current->p_value != value)

note that struct Obj 's member p_value is of type pointer-to-struct,
whereas value is type int.

If you need a generic pointer type, try void*.
program1.c:79: warning: passing arg 1 of `strcmp' from incompatible

This tells you that you need to pass the right type of argument to
strcmp. It expets a pointer to a null-terminated string. I suspect
its this line:

struct Obj* current=l;
while(current != NULL && !strcmp(current->p_value, value))

again, p_value is not a char*. If you want a char*, pass one.


Mark McIntyre
 
K

Keith Thompson

Ian Collins said:
You are missing <strings.h> which doesn't help.

I think you mean <string.h>. (Some systems do have a <strings.h>
header, but it's non-standard.)
 
C

CBFalconer

Could someone help me try to fix these errors on this code. Thanks!!

.... snip ...

You are much more likely to get someone to look at it if it is
properly formatted with reasonable indentation. Keep the
indentation level at 3 or 4 spaces, do not use tabs, and ensure
that total line lengths do not exceed about 65 chars (for usenet
posting). Also avoid // comments. Make sure the result is
compilable. As it stands I, for one, simply skip over it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top