PDA

View Full Version : [C] strtok e qsort


Joker91
26-03-2012, 17:31
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:

tok = strtok(p, ';');

passing argument 2 of ‘strtok’ makes pointer from integer without a cast [enabled by default]
/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ì:

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è

typedef scheda{
autore_t* autore;} scheda_t;


p1 e p2 che passo nella compare sono appunto due puntatori a scheda_t

clockover
26-03-2012, 18:26
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

clockover
26-03-2012, 18:30
Prova così
((Scheda_t*)p1)->autore

Joker91
26-03-2012, 19:15
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:



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]

clockover
26-03-2012, 19:50
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

pabloski
26-03-2012, 21:24
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!!!

clockover
26-03-2012, 23:42
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:



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]

per trovare gli errori dovresti postare almeno tutte le tue struct...