-
c program error
hello,
can someone help in removing these errors from my c code:
prog.h code:
Code:
typedef struct iosig
{
int signal_name;
int signal_sub;
}IoSignal;
typedef struct contsig
{
int diff; /* 1, 2, etc. 0 if no derivative */
int power; /* 2, 3, etc. 1 if no power */
IoSignal iosig;
struct contsig *next;
}ContSig;
typedef struct contterm
{
double coeff;
ContSig *csig, *csig_tail; /* could be NULL if just const coeff */
struct contterm *next;
}ContTerm;
typedef struct Conteqn
{
double coeff;
ContTerm *cterm, *cterm_tail; /* could be NULL if just const coeff */
struct conteqn *next;
}ContEqn;
extern ContEqn *new_conteqn(int i,ContTerm *cterm);
extern ContTerm *new_cterm( ContSig *csig);
extern ContSig * new_csig ( int i,IoSignal iosig,int j);
extern IoSignal iosign( int i,int j);
extern void append_conteqn( ContEqn *conteqn,ContTerm *cterm);
extern ContEqn* append_cterm( ContEqn *conteqn, int sig ,ContTerm *cterm);
extern void append_csig( ContTerm *cterm,ContSig *csig);
prog.c code starting from line 44
Code:
Conteqn *new_conteqn(int i,ContTerm *cterm)
{
struct Conteqn *e = malloc( sizeof( struct conteqn ));
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->coeff = i;
e->cterm = e->cterm_tail = cterm;
e->next = NULL;
return e;
}
void append_conteqn( Conteqn *conteqn,ContTerm *cterm)
{
conteqn->cterm_tail->next =cterm;
conteqn->cterm_tail=cterm;
}
ContTerm *new_cterm( ContSig *csig)
{
struct contterm *e = malloc( sizeof( struct ContTerm) );
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->csig = e->csig_tail = csig;
e->next = NULL;
return e;
}
void append_cterm( ContTerm *cterm, int sig, ContSig *csig)
{
cterm->csig_tail->next = csig;
cterm->csig_tail = csig;
cterm->coeff = sig;
}
ContSig * new_csig ( int i, IoSig iosig, int j)
{
struct contsig *e = malloc( sizeof( struct ContSig));
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->iosig = iosig;
e->diff=i;
e->power =j;
e->next = NULL;
return e;
}
void append_csig( ContSig *csig , IoSignal* iosig )
{
csig->iosig.next = iosig;
csig->iosig = iosig;
csig->diff = csig->diff;
csig->power = csig->power;
}
IoSignal iosign( int i,int j)
{
IoSignal x;
x.signal_name = i;
x.signal_sub = j;
return x;
}
errors:
jpj.c: At top level:
jpj.c:44: parse error before '*' token
jpj.c:45: warning: return type defaults to `int'
jpj.c:45: conflicting types for `new_conteqn'
jpj.h:52: previous declaration of `new_conteqn'
jpj.c: In function `new_conteqn':
jpj.c:46: sizeof applied to an incomplete type
jpj.c:55: warning: return from incompatible pointer type
jpj.c: At top level:
jpj.c:58: parse error before '*' token
jpj.c: In function `append_conteqn':
jpj.c:59: number of arguments doesn't match prototype
jpj.h:60: prototype declaration
jpj.c:60: `conteqn' undeclared (first use in this function)
jpj.c:60: `cterm' undeclared (first use in this function)
jpj.c: In function `new_cterm':
jpj.c:67: sizeof applied to an incomplete type
jpj.c: At top level:
jpj.c:80: conflicting types for `append_cterm'
jpj.h:62: previous declaration of `append_cterm'
jpj.c:86: parse error before "IoSig"
jpj.c: In function `new_csig':
jpj.c:87: number of arguments doesn't match prototype
jpj.h:56: prototype declaration
jpj.c:88: sizeof applied to an incomplete type
jpj.c:94: `iosig' undeclared (first use in this function)
jpj.c:95: `i' undeclared (first use in this function)
jpj.c:96: `j' undeclared (first use in this function)
jpj.c: At top level:
jpj.c:102: conflicting types for `append_csig'
jpj.h:64: previous declaration of `append_csig'
jpj.c: In function `append_csig':
jpj.c:103: structure has no member named `next'
jpj.c:104: incompatible types in assignment
regards,
cutelucks
-
I'll help you with the first thing I saw:
Code:
Conteqn *new_conteqn(int i,ContTerm *cterm)
What type is Conteqn? This is your declaraction of the function:
Code:
extern ContEqn *new_conteqn(int i,ContTerm *cterm);
See the problem?
*cough* Case sensitivity. *cough*.
-
And one more here which i can see ;)
Code:
typedef struct contterm
{
double coeff;
ContSig *csig, *csig_tail; /* could be NULL if just const coeff */
struct contterm *next;
}ContTerm;
Code:
ContTerm *new_cterm( ContSig *csig)
{
struct contterm *e = malloc( sizeof( struct ContTerm) );
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->csig = e->csig_tail = csig;
e->next = NULL;
return e;
}
ssharish
-
Got a problem here too:
Code:
extern void append_conteqn( ContEqn *conteqn,ContTerm *cterm);
Code:
void append_conteqn( Conteqn *conteqn,ContTerm *cterm)
Todd
-
on lines 44-45:
Code:
Conteqn *new_conteqn(int i,ContTerm *cterm)
{
Where are your function prototypes? because one of the errors is telling you that the prototype and function return or arguments don't match.
EDIT: Sorry, I don't know how I missed the prototypes. The problem is - as I said before - that they don't match.
compare the two - the first one above - :
Code:
extern ContEqn *new_conteqn(int i,ContTerm *cterm);
You see any difference? again *cough* case sensitivity *cough*
-
hiee
i corrected that error but still even when the names are consistent i get many more errors which i am unable to figure out.can anyone tell what else is wrong in my code?
thanx & Regards,
cutelucks
-
Well, post your latest code and the errors you get whilst compiling it.
-
-
my code
jpj.h
Code:
/* dynamically copy string or exit if malloc fails */
char *estrdup( const char *str);
/* have to declare this somewhere... */
extern void yyerror( const char *msg);
typedef struct iosig
{
int signal_name;
int signal_sub;
}IoSignal;
typedef struct contsig
{
int diff; /* 1, 2, etc. 0 if no derivative */
int power; /* 2, 3, etc. 1 if no power */
IoSignal iosig;
struct contsig *next;
}ContSig;
typedef struct contterm
{
double coeff;
ContSig *csig, *csig_tail; /* could be NULL if just const coeff */
struct contterm *next;
}ContTerm;
typedef struct Conteqn
{
double coeff;
ContTerm *cterm, *cterm_tail; /* could be NULL if just const coeff */
struct conteqn *next;
}ContEqn;
extern ContEqn *new_conteqn(int i,ContTerm *cterm);
extern ContTerm *new_cterm( ContSig *csig);
extern ContSig * new_csig ( int i,IoSignal iosig,int j);
extern IoSignal iosign( int i,int j);
extern void append_conteqn( ContEqn *conteqn,ContTerm *cterm);
extern void append_cterm( ContEqn *conteqn, int sig ,ContTerm *cterm);
extern void append_csig( ContTerm *cterm,ContSig *csig);
void print_fn();
jpj.c :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "jpj.h"
/* dynamically copy string or exit if malloc fails */
char *estrdup( const char *str)
{
char *s = malloc( strlen(str) + 1);
if( s == NULL)
{
yyerror( "estrdup: malloc failed");
exit(1);
}
strcpy( s, str);
return s;
}
ContEqn *new_conteqn(int i,ContTerm *cterm)
{
struct ContEqn *e = malloc( sizeof( struct ContEqn ));
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->coeff = i;
e->cterm = e->cterm_tail = cterm;
e->next = NULL;
return e;
}
void append_conteqn( ContEqn *conteqn,ContTerm *cterm)
{
conteqn->cterm_tail->next =cterm;
conteqn->cterm_tail=cterm;
}
ContTerm *new_cterm( ContSig *csig)
{
struct ContTerm *e = malloc( sizeof( struct ContTerm) );
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->coeff = e->coeff;
e->csig = e->csig_tail = csig;
e->next = NULL;
return e;
}
void append_cterm( ContEqn *conteqn, int sig ,ContTerm *cterm)
{
conteqn->cterm_tail->next =cterm;
conteqn->cterm_tail=cterm;
conteqn->coeff = sig;
conteqn->next = NULL;
}
ContSig * new_csig ( int i, IoSig iosig, int j)
{
struct ContSig *e = malloc( sizeof( struct ContSig));
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->iosig = iosig;
e->diff=i;
e->power =j;
e->next = NULL;
return e;
}
void append_csig(ContTerm *cterm,ContSig *csig)
{
cterm->csig_tail->next = csig;
cterm->csig_tail = csig;
cterm->next = NULL;
cterm->coeff = cterm->coeff;
}
IoSignal iosign( int i,int j)
{
IoSignal x;
x.signal_name = i;
x.signal_sub = j;
return x;
}
void print_fn()
{
printf("dummy");
}
Regards,
cutelucks
-
errors:
hiie,
sorry i missed the errors part.
Errors:
jpj.c: In function `new_conteqn':
jpj.c:46: sizeof applied to an incomplete type
jpj.c:52: dereferencing pointer to incomplete type
jpj.c:53: dereferencing pointer to incomplete type
jpj.c:53: dereferencing pointer to incomplete type
jpj.c:54: dereferencing pointer to incomplete type
jpj.c:55: warning: return from incompatible pointer type
jpj.c: In function `new_cterm':
jpj.c:67: sizeof applied to an incomplete type
jpj.c:73: dereferencing pointer to incomplete type
jpj.c:73: dereferencing pointer to incomplete type
jpj.c:74: dereferencing pointer to incomplete type
jpj.c:74: dereferencing pointer to incomplete type
jpj.c:75: dereferencing pointer to incomplete type
jpj.c:76: warning: return from incompatible pointer type
jpj.c: At top level:
jpj.c:81: conflicting types for `append_cterm'
jpj.h:62: previous declaration of `append_cterm'
jpj.c:89: parse error before "IoSig"
jpj.c: In function `new_csig':
jpj.c:90: number of arguments doesn't match prototype
jpj.h:56: prototype declaration
jpj.c:91: sizeof applied to an incomplete type
jpj.c:97: dereferencing pointer to incomplete type
jpj.c:97: `iosig' undeclared (first use in this function)
jpj.c:98: dereferencing pointer to incomplete type
jpj.c:98: `i' undeclared (first use in this function)
jpj.c:99: dereferencing pointer to incomplete type
jpj.c:99: `j' undeclared (first use in this function)
jpj.c:100: dereferencing pointer to incomplete type
jpj.c:101: warning: return from incompatible pointer type
jpj.c starts from line 46
-
Your first few errors are here.
Code:
ContTerm *new_cterm( ContSig *csig)
{
struct ContTerm *e = malloc( sizeof( struct ContTerm) );
if( e == NULL)
{
yyerror( "new_node: malloc failed");
exit(1);
}
e->coeff = e->coeff;
e->csig = e->csig_tail = csig;
e->next = NULL;
return e;
}
The compiler is complaining because the type struct ContTerm doesn't exist. If you declare a structure like this
Code:
typedef struct {
/* ... */
} newtype;
then you can only use newtype, not struct newtype.
In your case,
Code:
typedef struct contterm
{
double coeff;
ContSig *csig, *csig_tail; /* could be NULL if just const coeff */
struct contterm *next;
}ContTerm;
you can use the types "ContTerm" and "struct contterm".
You should either use a typedef and no "struct"s, or don't use a typedef and use "struct"s everywhere. Your choice. You could use both, but it's just plain confusing.
-
The first error, I think, was pointed out earlier. There is no struct called "ContEqn" - there is a typedef by that name, but no struct. Compiler can't tell the difference between "You added struct in front of a typedef" and "I haven't heard of a struct called ContEqn".
General rule when it comes to fixing errors [aside from "Don't write lots of code then try to compile it"] is to fix the first error, then compile again. I suspect you will find that sometimes a whole bunch of errors are caused by a single mistake in the code.
--
Mats
-
thanx a lot.i could correct most of my errors now.i have only one more doubt.what does this error mean?
jpj.c: In function `new_conteqn':
jpj.c:46: sizeof applied to an incomplete type
jpj.c: In function `new_cterm':
jpj.c:67: sizeof applied to an incomplete type
thanx & Regards,
cutelucks
-
The same thing, probably.
Code:
#include <stdio.h>
int main() {
printf("%d\n", (int)sizeof(struct this_does_not_exist));
return 0;
}
Code:
c.c: In function ‘main’:
c.c:4: error: invalid application of ‘sizeof’ to incomplete type ‘struct this_does_not_exist’
-