Avevo mischiato un po' i codici ....resta il fatto che ho problemi a capire i cast !Originariamente inviato da ramy89
Non si capisce il contesto, cos'è ps e cos'è pt? Posa il codice completo.
a che serve il void ,il const ....se poi li togli e funziona lo stesso...
Grazie.
Il codice se lo entro col CODE è un unica riga lunga !
Per farmi capire ....ho fatto dei tentativi per farmi entrare in testa la funzione del cast ...togliendo
asterischi e capirne il significato dai messaggi di errore . Ma un po' per l 'inglese un po' per me
fatico a capire anche i messaggi che dovrebbero aiutarmi ! Cosa vogliono dire >>>>>
./veforum.c: In function ‘cmpstringp’:
../veforum.c:93: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
return strcmp(* (char * ) p1, * (char * *) p2);
./veforum.c: In function ‘cmpstringp’:
../veforum.c:93: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type
return strcmp( (char * *) p1, * (char * *) p2);
../veforum.c:93: warning: dereferencing ‘void *’ pointer
../veforum.c:93: error: invalid use of void expression
return strcmp(* p1, * (char * *) p2);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINES 1000
#define _GNU_SOURCE /* per definire usi getline e altro, in libc */
char *lineptr[MAXLINES]; /* puntatore alle linee di testo */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
int cmpstringp(const void *p1, const void *p2); /* funzione per qsort libc */
int main(int argc, char **argv)
{
int i;
i = 0;
int nlines;
if ((nlines = readlines(lineptr, MAXLINES)) >= 0 )
{
qsort(lineptr, nlines, sizeof (lineptr[0]),cmpstringp);
writelines(lineptr, nlines);
return 0 ;
}
else
{
printf("errore: troppe linee di input da ordinare\n");
return 1 ;
}
}
/* readlines legge le linee di input */
#define ALLOCSIZE 10000
#define MAXLEN 1000
int readlines(char *lineptr[], int maxlines )
{
char *p;
int nlines = 0;
int lungh = 0;
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, stdin)) > 0 )
{
if ((p = (char *) malloc (len)) == NULL) /* io ho messo len come allocazione memoria */
return -1 ;
else
{
lungh = strlen (line);
line[lungh - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
}
free (line);
free (p);
return (nlines);
}
/* writelines scrive in output le linee */
void writelines(char *lineptr[], int nlines)
{
int i;
for (i = 0; i < nlines; i++ )
printf("%s\n", lineptr[i]);
}
int cmpstringp(const void *p1, const void *p2)
{
return strcmp(* (char * *) p1, * (char * *) p2);
}