Gmail Calendar Documenti Reader Web altro »
Gruppi visitati di recente | Guida | Entra
Home page di Google Gruppi
qsort variable length array of variable length strings
Al momento sono presenti troppi argomenti in questo gruppo da visualizzare per primi. Per visualizzare questo argomento per primo, rimuovi questa opzione da un altro argomento.
Si è verificato un errore durante l'elaborazione della tua richiesta. Riprova.
contrassegno
  5 messaggi - Comprimi tutto  -  Traduci tutto in Tradotto (Visualizza tutti gli originali)
Il gruppo nel quale stai postando è un gruppo Usenet. I messaggi postati in questo gruppo rendono la tua email visibile a chiunque su Internet
Il messaggio di risposta non è stato inviato.
Post riuscito
 
Da:
A:
Cc:
Risposta a:
Aggiungi Cc | Aggiungi Followup-to | Modifica oggetto
Oggetto:
Convalida:
A scopo di verifica, digita i caratteri visualizzati nell'immagine qui di seguito o i numeri pronunciati quando fai clic sull'icona per l'accesso facilitato. Ascolta e digita i numeri che senti
 
Rich  
Vedi profilo   Traduci in Tradotto (Visualizza originale)
 Altre opzioni 30 Gen, 21:12
Newsgroup: comp.lang.c.moderated
Da: Rich <rtillm...@gmail.com>
Data: Sat, 30 Jan 2010 14:12:37 -0600 (CST)
Locale: Sab 30 Gen 2010 21:12
Oggetto: qsort variable length array of variable length strings
Hi

I am trying to use qsort to a dynamically allocated array of strings.
It has two problems:

1) it doesn't sort
2) it gives garbage when using long data
if I use ./prog1 red blue green
warray = red
warray = blue
warray = green

./prog1 red blue green ddddddddddddddddddddddddddddddd
warray = (null)
warray = (null)
warray = (null)
warray = (null)

What did I do wrong?

Thanks,

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main (int argc, char *argv[])
{
char **warray;
size_t numleft;
size_t temp;
size_t lhold;
size_t lstr=0;

  numleft=(size_t)(argc-1); /* number of words to put in array */
  warray=malloc(numleft * sizeof(*warray));

  if (warray == NULL) {
    printf("can't allocate memory for warray\n");
    exit(EXIT_FAILURE);
  }
  for (temp=0; temp < numleft; temp++) { /* get length of longest word
*/
    lhold = strlen (argv[temp+1]);
    if (lhold > lstr)
      lstr = lhold;
    warray[temp] = argv[temp+1]; /* put argument into array */
  }
  qsort(warray, lstr, numleft, strcmp);

  for (lstr=0;lstr<numleft;lstr++) {
    printf("warray = %s\n",warray[lstr]);
  }
return 0;

}

--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

    Inoltra  
Devi eseguire l'accesso prima di poter postare i messaggi.
Per pubblicare un messaggio è necessario, innanzitutto, partecipare a questo gruppo.
Aggiorna il tuo nickname nella pagina delle impostazioni dell'iscrizione prima di postare i messaggi.
Non sei autorizzato a postare messaggi.
Seebs  
Vedi profilo   Traduci in Tradotto (Visualizza originale)
 Altre opzioni 30 Gen, 22:05
Newsgroup: comp.lang.c.moderated
Da: Seebs <usenet-nos...@seebs.net>
Data: Sat, 30 Jan 2010 15:05:50 -0600 (CST)
Locale: Sab 30 Gen 2010 22:05
Oggetto: Re: qsort variable length array of variable length strings
On 2010-01-30, Rich <rtillm...@gmail.com> wrote:

> 1) it doesn't sort

I suspect that you're missing a layer of indirection.

qsort() calls its comparison function with the ADDRESSES of two objects
in the array.  The array you have is an array of character pointers,
so you're actually getting the addresses of those pointers passed to
strcmp, which ought to be pretty much pure garbage.

Suggestion:
        int ptr_to_strcmp(void *v1, void *v2) {
                const char **s1 = v1;
                const char **s2 = v2;
                return strcmp(*s1, *s2);
        }

-s
--
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.


    Inoltra  
Devi eseguire l'accesso prima di poter postare i messaggi.
Per pubblicare un messaggio è necessario, innanzitutto, partecipare a questo gruppo.
Aggiorna il tuo nickname nella pagina delle impostazioni dell'iscrizione prima di postare i messaggi.
Non sei autorizzato a postare messaggi.
Rich  
Vedi profilo   Traduci in Tradotto (Visualizza originale)
 Altre opzioni 31 Gen, 00:31
Newsgroup: comp.lang.c.moderated
Da: Rich <rtillm...@gmail.com>
Data: Sat, 30 Jan 2010 17:31:30 -0600 (CST)
Locale: Dom 31 Gen 2010 00:31
Oggetto: Re: qsort variable length array of variable length strings
On Jan 30, 3:05 pm, Seebs wrote:

Thank you.  That and a small change made the program work.

Thanks again.
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.


    Inoltra  
Devi eseguire l'accesso prima di poter postare i messaggi.
Per pubblicare un messaggio è necessario, innanzitutto, partecipare a questo gruppo.
Aggiorna il tuo nickname nella pagina delle impostazioni dell'iscrizione prima di postare i messaggi.
Non sei autorizzato a postare messaggi.
Barry Schwarz  
Vedi profilo   Traduci in Tradotto (Visualizza originale)
 Altre opzioni 31 Gen, 05:50
Newsgroup: comp.lang.c.moderated
Da: Barry Schwarz <schwa...@dqel.com>
Data: Sat, 30 Jan 2010 22:50:20 -0600 (CST)
Locale: Dom 31 Gen 2010 05:50
Oggetto: Re: qsort variable length array of variable length strings
On Sat, 30 Jan 2010 14:12:37 -0600 (CST), Rich <rtillm...@gmail.com>
wrote:

What do you think the cast accomplishes?

>  warray=malloc(numleft * sizeof(*warray));

>  if (warray == NULL) {
>    printf("can't allocate memory for warray\n");
>    exit(EXIT_FAILURE);
>  }
>  for (temp=0; temp < numleft; temp++) { /* get length of longest word
>*/
>    lhold = strlen (argv[temp+1]);
>    if (lhold > lstr)
>      lstr = lhold;
>    warray[temp] = argv[temp+1]; /* put argument into array */
>  }
>  qsort(warray, lstr, numleft, strcmp);

warray is an array of pointers, not an array of strings.  qsort will
pass the address of two array elements to strcmp.  Therefore strcmp
will not correctly process the addresses qsort passes it since it
expects to receive pointers to strings, not pointers to pointers.

Your second argument is supposed to be the size of each element of
warray.  It isn't.  It is the size of the longest string any element
of warray points to and is completely unrelated to the size of the
element itself.

>  for (lstr=0;lstr<numleft;lstr++) {
>    printf("warray = %s\n",warray[lstr]);
>  }
>return 0;
>}

--
Remove del for email
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

    Inoltra  
Devi eseguire l'accesso prima di poter postare i messaggi.
Per pubblicare un messaggio è necessario, innanzitutto, partecipare a questo gruppo.
Aggiorna il tuo nickname nella pagina delle impostazioni dell'iscrizione prima di postare i messaggi.
Non sei autorizzato a postare messaggi.
lawrence.jo...@siemens.com  
Vedi profilo   Traduci in Tradotto (Visualizza originale)
 Altre opzioni 31 Gen, 05:51
Newsgroup: comp.lang.c.moderated
Da: lawrence.jo...@siemens.com
Data: Sat, 30 Jan 2010 22:51:22 -0600 (CST)
Locale: Dom 31 Gen 2010 05:51
Oggetto: Re: qsort variable length array of variable length strings

Rich <rtillm...@gmail.com> wrote:

>   qsort(warray, lstr, numleft, strcmp);

In addition to what Seebs said, this call is defective, too.  You've got
the size and number of elements reversed and the size should just be the
size of an array element (there's no need to compute lstr):

        qsort(warray, numleft, sizeof *warray, mycomp);
--
Larry Jones

You should see me when I lose in real life! -- Calvin
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.


    Inoltra  
Devi eseguire l'accesso prima di poter postare i messaggi.
Per pubblicare un messaggio è necessario, innanzitutto, partecipare a questo gruppo.
Aggiorna il tuo nickname nella pagina delle impostazioni dell'iscrizione prima di postare i messaggi.
Non sei autorizzato a postare messaggi.
Fine dei messaggi
« Torna alle discussioni « Argomento più recente     Argomento meno recente »

Crea un gruppo - Google Gruppi - Home page di Google - Termini di servizio - Norme sulla privacy
©2010 Google