Re: [Fwd: How to avoid cc warnings?]
Posted in 1998
On Thu, 10 Sep 1998, Evelio Mart=EDnez wrote: > Este es un mensaje multipartes en formato MIME. > --------------D0E565BC11B526572CBC2634 > Content-Type: text/plain; charset=3Diso-8859-1 > Content-Transfer-Encoding: 8bit >=20 >=20 > #include <stdio.h> > #include <search.h> > #include <tipos.h> >=20 > #define NARBOLES 75 > #define NNODOS 500 /* no se usa */ > #define LONCLAVE 50 > #define LONDATO 513 >=20 > /* TIPO NODO : consta de dos punteros : uno a la clave y otro al dato */ >=20 > struct nodo { char *clave, *dato; } ; >=20 > struct nodo > *arboles[NARBOLES] ; >=20 > /* numero de ultima entrada utilizada en vector de arboles */ > short uarbol =3D -1 ; >=20 > struct nodo **secuencia ; > short isec ; >=20 > char *malloc(), *consultar( short, char * ); Slap, slap, slap! Don't ever do that (declare malloc()). Use: #include <stdlib.h> to declare it. The correct prototype is: extern void *malloc(size_t); > void twalk(const void *, void (*)(const void *, VISIT, int)); That should be declared by search.h, but using search.h ties you to System V platforms. It is not standardized by ISO/ANSI or POSIX (though it is part of Unix 98). > /*extern void twalk(const void *, void (*)(const void *, VISIT, int));*/ What benefit do you get from having this comment? > /********************************** COMPANODOS */ > int companodos( n1, n2 ) > struct nodo *n1, *n2 ; > { return strcmp( n1->clave, n2->clave ) ; } #include <string.h> to get the correct declaration of strcmp(). The function is used as the last argument to tsearch(); the correct prototype for such a function is: =09int companodos(const void *p1, const void *p2); The contents then have to be fixed up accordingly. I suspect that you are being passed a pointer to a pointer to a struct nodo cast as void *, so your code might need to be modified to avoid the core dumps. On the other hand, you may be OK because I haven't battered my brain against what your code is doing. You should certainly test the companodos() function via tsearch() in isolation from the I4GL code, and ensure that you are processing the correct data (by putting diagnostic printing into companodos() to check that the two structures are accurate). > /********************************** MUESTRA */ > void muestra( nodo, orden, nivel ) > struct nodo **nodo; > VISIT orden; > int nivel; > { > if ( orden =3D=3D preorder || orden =3D=3D leaf ) > printf( " Puntero =3D %d, Clave =3D %s, Dato =3D %s\\n\\r", *nodo, > (*nodo)->clave, (*nodo)->dato ) ; > } >=20 >=20 > /********************************** C_VERARBOLES */ C9x will outlaw the implicit int decalaration. > c_verarboles() { > short i ; >=20 > puts("\\r"); > for( i=3D0 ; i<=3Duarbol ; i++ ) { > printf( "ARBOL %d : %d : %d \\n\\r", i, &arboles[i], arboles[i] ); > puts("\\r"); > twalk( (char *)arboles[i], muestra ); > puts("\\r"); > } > puts(""); > return(0); > } >=20 > /*********************** C_CREAARBOL */ >=20 > c_creaarbol() { > uarbol++ ; > if ( uarbol+1 =3D=3D NARBOLES ) > printf("%s %d","AVISO: Alcanzando num. max. arboles",NARBOLES); > arboles[ uarbol ] =3D NULL ; > retint( uarbol ) ; > return(1) ; > } >=20 > /*********************** C_INSNODO */ >=20 > c_insnodo() This is the incorrect interface for an I4GL callable C function. It should be: int c_insnodo(int nargs) > { > ENTERO_POPINT arbol ; > char clave[LONCLAVE], dato[LONDATO]; > struct nodo *p, *p2; The popquote() function is declared in fglsys.h in more recent versions of I4GL (say 4.13/6.01 upwards). Ditto for the other popxyz and the retxyz functions. #include <fglsys.h> > /* Regcoger parametros */ > popquote( dato, sizeof(dato) ); cortatira( dato ); > popquote( clave, sizeof(clave) ); cortatira( clave ); > popint( &arbol ) ; >=20 > /* Reservar espacio de memoria para el nodo, la clave y el dato */ > p =3D (struct nodo *)malloc( sizeof(struct nodo) ) ; > p->clave =3D malloc( strlen(clave) + 1 ) ; strcpy( p->clave, clave ) ; > p->dato =3D malloc( strlen(dato) + 1 ) ; strcpy( p->dato, dato ) ; >=20 > /* insertar/actualizar arbol */ > p2 =3D (struct nodo *) > tsearch( (char *)p, &(char *)arboles[ arbol ], companodos ) ; > if ( p =3D=3D p2 ) printf(" Ya existe esa clave ! " ); > return(0); > } >=20 > /*********************** C_BORRNODO */ >=20 > c_borrnodo() Wrong interface. > { > ENTERO_POPINT arbol ; > char clave[LONCLAVE]; > struct nodo n, **pn =3D NULL, *pn2 ; >=20 > /* Regcoger parametros */ > popquote( clave, sizeof(clave) ); cortatira( clave ); > popint( &arbol ) ; >=20 > n.clave =3D clave ; > pn =3D > (struct nodo **)tfind( (char *)&n, &(char *)arboles[arbol], companodos > ) ; That is a very weird second argument. It is one source of complaints. You probably meant (char *)&arboles[arbol], though since tfind() takes void * arguments, the char * cast is pretty pointless. As it is, you are passing the address of a char *, and casting a structure into a char *, and it is all gobbledegook. Repeat this comment ad nauseam for the other tfind()s and tsearch()s and td= elete()s. >=20 > if ( pn !=3D NULL ) > { > pn2 =3D *pn ; > free( pn2->clave ); > free( pn2->dato ); > pn =3D (struct nodo **) > tdelete( (char *)&n, &(char *)arboles[arbol], companodos ) ; > free( pn2 ); > } >=20 > return(0); > } >=20 > /*********************** C_CONSNODO */ > c_consnodo() Wrong interface. > { > ENTERO_POPINT arbol ; > char clave[LONCLAVE]; > struct nodo nodo, **p =3D NULL ; >=20 > /* Regcoger parametros */ > popquote( clave, sizeof(clave) ); cortatira( clave ); > popint( &arbol ) ; >=20 > nodo.clave =3D clave ; > p =3D (struct nodo **) > tfind( (char *)&nodo, &(char *)arboles[arbol], companodos ) ; >=20 > if ( p =3D=3D NULL ) > retquote(NULL) ; > else > retquote( (*p)->dato ) ; >=20 > return(1); > } >=20 > /* > ############################################################### > RECORRIDO DEL ARBOL > ############################################################### > */ >=20 > /********************************** RECORRE */ > void recorre( nodo, orden, nivel ) > struct nodo **nodo; > VISIT orden; > int nivel; > { > if ( orden =3D=3D preorder || orden =3D=3D leaf ) > { > isec++ ; > secuencia[ isec ] =3D *nodo ; > } > return; > } >=20 > /*********************** C_ABREARBOL */ >=20 > c_abrearbol() { >=20 > ENTERO_POPINT arbol ; >=20 > popint( &arbol ); > secuencia =3D (struct nodo **)calloc( 20, sizeof( struct nodo * ) ) ; > isec =3D -1 ; > twalk( (char *)arboles[ arbol ], recorre ); > retint( isec+1 ); > return(1); > } >=20 > /*****