Re: How to fix this structure?

From: Al Bowers (xabowers_at_rapidsys.com)
Date: 05/18/04


Date: Mon, 17 May 2004 19:13:51 -0400


Chris R. wrote:
> Hi everyone. I am trying to finish my homework, but I seem not to
> figure out how to fix this structure that seems to make wrong output.
> What this program should do is use structure to save the word and line
> number on which it was in array pointer using malloc and print them
> out alphabetized (no duplicates).
>
> Most of it works, except the words seem to be corrupted. Here is a
> code, if anyone can help me it would help so much since I've been
> working on this for a really long time. I tried even changing '\0' to
> NULL, but then program errored out.
>
>
   .... code snipped ....

There are many errors, so I want take the time to try to fix your
code. Along with the errors you have a design problem with the
date type and with all the operations being done in one function, main.

I would make the data type List which contains a pointer to an
array of data type WORD as one member and a counter to keep a count
on the number of words in the list as the other member. In the
data type WORD you would store the word in on member and the other
member would be the line number.

Example:
typedef struct WORD
{
    unsigned linenum;
    char *name;
} WORD;

typedef struct LIST
{
    WORD *word;
    unsigned count;
} LIST;

Now you should write fucntions that do simple operations
on the LIST datatype. For example AddWordtoList() to add
a word, PrintList() to print the list, FreeList() to free
the allocations once you are finished with the list.
You could use Standard C functions bsearch to search the
list for dupes and function qsort to sort the list as you
build it.

An example of a possible definiion of AddWordtoList().

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

int cmp(const void *v1, const void *v2)
{
    const WORD *w1 = v1;
    const WORD *w2 = v2;

    return strcmp(w1->name,w2->name);
}

int AddWordtoList(LIST *p, const char *word, unsigned linenr)
{
    WORD *tmp,key;
    char *s, *s1;

    if((s = malloc(strlen(word)+1)) == NULL) return 0;
    strcpy(s,word);
    for(s1 = s; (*s1 = tolower((unsigned char)*s1));s1++) ;
    key.name = s;
    if(p->word != NULL)
       if(bsearch(&key,p->word,p->count,sizeof *p->word,cmp))
       { /* dupe */
          free(s);
          return 0;
       }
    tmp = realloc(p->word,(p->count+1)*(sizeof *tmp));
    if(tmp == NULL)
    {
       free(s);
       return 0;
    }
    p->word = tmp;
    p->word[p->count].name = s;
    p->word[p->count++].linenum = linenr;
    qsort(p->word,p->count,sizeof *p->word,cmp);
    return 1;
}

-- 
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Relevant Pages

  • Re: WMI
    ... Error Code 10 and fix ... >>> I can find one file that can NOT be repair ...
    (microsoft.public.windows.vista.general)
  • RE: Importing files from Excel
    ... only when importing from Excel. ... >This is a bug in Access 2003 and there is no fix yet ... format, name the fields / columns as needed. ... found there the data type. ...
    (microsoft.public.access.externaldata)
  • Re: Creating a measure using member properties
    ... "fsanchez" wrote: ... Analysis Services to aggregate the values. ... the raw data type or cast the string to a numeric data type... ... I have a DEAL dimension that has several member proproties (Base Rate, ...
    (microsoft.public.sqlserver.olap)
  • Re: persist an array of data
    ... and tell me how can I specify the data type of each array's element (and, ... the data type of each arrayList element)? ... > ArrayList which contains some certain class's instance's arraies? ... > them has a member, the member is a certain class's object array. ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: Cant See DVD-Burner
    ... I found the fix on Microsoft's support website - Microsoft Knowledge Base ... If you use Registry Editor incorrectly, ... If this data type is missing, ...
    (microsoft.public.windowsxp.general)