|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Feb 2007
Messaggi: 467
|
[C] strtok e qsort
ho dei problemi quando vado a compilare il programma (usando -Wall), ve li elenco così magari qualcuno riesce a dirmi il problema...
- Il primo esce quando uso la funzione strtok di string.h, in questo modo: Codice:
tok = strtok(p, ';'); /usr/include/string.h:348:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ Mi pare di capire che legge il punto e virgola come intero invece che come carattere? - Il secondo problema è quando uso il qsort, sto facendo sicuramente qualche errore con il casting... l'errore precisamente è nella funzione di comparazione che ho definito così: Codice:
int cmpautore(const void *p1, const void *p2) { int n; if (n= cmpstringp(*(scheda_t*)p1->autore->cognome, *(scheda_t*)p2->autore->cognome) != 0) return n; return cmpstringp(*(scheda_t*)p1->autore->nome, *(scheda_t*)p2->autore->nome); } warning: dereferencing ‘void *’ pointer [enabled by default] error: request for member ‘autore’ in something not a structure or union scheda_t è una struttura, autore è un puntatore a struttura dentro scheda_t... cioè Codice:
typedef scheda{ autore_t* autore;} scheda_t; |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Oct 2004
Messaggi: 1945
|
Per strtok devi usare i doppi apici nel definire il separatore http://www.manpagez.com/man/3/strtok/ , per l 'altro problema non riesco a leggere bene dato che sono con il cellulare
|
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Oct 2004
Messaggi: 1945
|
Prova così
((Scheda_t*)p1)->autore |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Feb 2007
Messaggi: 467
|
così mi toglie quel warning ma mi dà questo che comunque non è grave:
warning: suggest parentheses around assignment used as truth value però ho un altro problema, e cioè che devo fare una funzione compare per la qsort fra le struct scheda_t usando un loro valore numerico all'interno... E anche qui ho qualche problema col casting... Ecco cos'ho scritto io: Codice:
RIGA 611: int cmpnumero(const void *p1, const void *p2){ return (*(int*)p1 - *(int*)p2); } int cmpprestito(const void *p1, const void *p2){ int n; if (n= cmpnumero(((scheda_t*)p1)->prestito.scadenza.tm_year, ((scheda_t*)p2)->prestito.scadenza.tm_year) != 0) return n; if (n= cmpnumero(((scheda_t*)p1)->prestito.scadenza.tm_mon, ((scheda_t*)p2)->prestito.scadenza.tm_mon) != 0) return n; return cmpnumero(((scheda_t*)p1)->prestito.scadenza.tm_mday, ((scheda_t*)p2)->prestito.scadenza.tm_mday); RIGA 621: } "cmpnumero" non è altro che la compare fra interi... Alla fine il concetto dovrebbe essere lo stesso del primo esempio, semplicemente qui ho degli interi e lì una stringa. Però mi dà errori diversi: bib.c: In function ‘cmpprestito’: bib.c:618:2: warning: passing argument 1 of ‘cmpnumero’ makes pointer from integer without a cast [enabled by default] bib.c:611:5: note: expected ‘const void *’ but argument is of type ‘int’ bib.c:618:2: warning: passing argument 2 of ‘cmpnumero’ makes pointer from integer without a cast [enabled by default] bib.c:611:5: note: expected ‘const void *’ but argument is of type ‘int’ bib.c:618:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bib.c:619:2: warning: passing argument 1 of ‘cmpnumero’ makes pointer from integer without a cast [enabled by default] bib.c:611:5: note: expected ‘const void *’ but argument is of type ‘int’ bib.c:619:2: warning: passing argument 2 of ‘cmpnumero’ makes pointer from integer without a cast [enabled by default] bib.c:611:5: note: expected ‘const void *’ but argument is of type ‘int’ bib.c:619:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses] bib.c:620:2: warning: passing argument 1 of ‘cmpnumero’ makes pointer from integer without a cast [enabled by default] bib.c:611:5: note: expected ‘const void *’ but argument is of type ‘int’ bib.c:620:2: warning: passing argument 2 of ‘cmpnumero’ makes pointer from integer without a cast [enabled by default] bib.c:611:5: note: expected ‘const void *’ but argument is of type ‘int’ bib.c: In function ‘ricerca_tag’: bib.c:657:1: warning: control reaches end of non-void function [-Wreturn-type] Ultima modifica di Joker91 : 26-03-2012 alle 19:33. |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Oct 2004
Messaggi: 1945
|
Sempre perché non riesco a leggere bene non posso correggerti direttamente ma questo link dovrebbe aiutarti
http://gcc.gnu.org/ml/gcc/1998-07/msg00085.html |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jan 2008
Messaggi: 8406
|
Vorrei chiarire il perchè nel primo caso si comportava così. Quando usi gli apici singoli ( mettendo tra di essi un singolo carattere ) stai in realtà passando un tipo char, cioè un valore numerico. Lo so si chiama char, ma in realtà è un numero non una stringa di un solo elemetno.
Stesso discorso vale nel secondo caso, solo che lì hai messo come parametri d'input 2 puntatori ma poi gli passi degli interi? In pratica prima fai un passaggio per riferimento/puntatore e poi passi il parametro per valore? E' un erroraccio!!! |
![]() |
![]() |
![]() |
#7 | |
Senior Member
Iscritto dal: Oct 2004
Messaggi: 1945
|
Quote:
|
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:28.