Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not compiling!? why these couple bugs ? Please help - I am lost

Status
Not open for further replies.

zmiek

Programmer
Jun 25, 2002
2
0
0
CA
I got the program in place , but only two errors preventing me from compiling it..

line 63:
left operand must be modifiable lvalue: op "="
line 66:
left operand must be modifiable lvalue: op "="
cc: acomp failed for ar2.c

Where did I go wrong? Please see attached
Please help me successfully complie...Thank you very much!!!!
CODE:
______________________________________________________________________

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

#define SPELL_OKAY 0 /* success */
#define SPELL_FAILURE -1 /* general, unspecified failure */
#define SPELL_NOMEMORY 1 /* could not allocate more memory */
#define SPELL_NOTFOUND 2 /* could not find a file */
#define SPELL_TOOLONG 3 /* a word is too long */
#define SPELL_INCORRECT 4 /* a word is incorrect */
#define MAXLENGTH 64
#define EXTEND_SIZE 20


/*Function declarations*/
int max_word_len(void);
int add_word(char *word);
void forget_word(char *word);
void forget_all(void);
int read_dict(char *file);
int check_word(char *word);
int check_file(char *file, void (*badword)(char *));
int extend_dictionary ( void );
char *dup_string ( char *word );
int compare_fn ( const void *a, const void *b );
int binsearch( char *word, char *dict[], int n);
void swap(char *v[], int i, int j);
void quicksort(char *v[], int left, int right);

char *dictionary[] = {NULL};
int num_words = 0;
int max_words = 0;


int main ( )
{
int i;

add_word( &quot;Now&quot; );
add_word( &quot;is&quot; );
add_word( &quot;the&quot; );
add_word( &quot;time&quot; );
add_word( &quot;for&quot; );
add_word( &quot;all&quot; );
add_word( &quot;good&quot; );

for ( i = 0 ; i < num_words ; i++ ) {
printf( &quot;Word %d is %s\n&quot;, i, dictionary );
}
return 0;
}


/* extends the dictionary in steps of 20 words */
int extend_dictionary (void)
{
char* temp[] = {NULL};
int new_size;

if ( num_words == max_words )
{
new_size = max_words + EXTEND_SIZE;
temp = (char *)realloc(dictionary, new_size * sizeof(char) );
if ( temp != NULL )
{
dictionary = temp;
max_words = new_size;
}
else
return SPELL_NOMEMORY;
}
return SPELL_OKAY;
}


/* make a copy of a string */
char *dup_string ( char *word )
{
char *temp = malloc( strlen(word) + 1 );

if ( temp != NULL )
{
strcpy( temp, word );
return temp;
}
else
return NULL;
}


int add_word ( char *word )
{ int iptr;
char *ptr;

if( strlen(word) > max_word_len() )
return SPELL_TOOLONG;
/*check if the word is in the dictionary*/
if (check_word(word) == SPELL_OKAY)
return SPELL_OKAY;
else
{ if ( (ptr = dup_string(word))== NULL )
return SPELL_NOMEMORY;
else if ( (iptr = extend_dictionary()) == SPELL_NOMEMORY )
return SPELL_NOMEMORY;
else
{
dictionary[num_words++] = dup_string( word );
quicksort( dictionary, 0, num_words-1);
return SPELL_OKAY;
}
}
}


int max_word_len(void)
{ return MAXLENGTH;
}



int check_word(char *word)
{
int i, len;
int bNumeric = 1;
int wordnum = 0;

/*check for length*/
len = strlen(word);
if ( len > max_word_len())
return SPELL_TOOLONG;

for (i=0; i<len; i++)
{
/*not a digit*/
if ( isdigit(word) == 0)
{
bNumeric = 0;
break;
}
}
if (bNumeric == 1)
return SPELL_OKAY;
else
{
wordnum = binsearch(word, dictionary, num_words);
if ( wordnum != -1)
return SPELL_OKAY;
else
return SPELL_INCORRECT;
}
}


int binsearch( char *word, char *dict[], int n)
{ int low, high, mid;
low=0;
high = n-1;

while(low <=high)
{ mid = (low + high)/2;
if ( compare_fn(word, dict[mid]) < 0)
high = mid -1;
else if ( compare_fn(word, dict[mid]) > 0 )
low=mid+1;
else
return mid;
}
return -1;
}


void quicksort(char *v[], int left, int right)
{ int i, last;
void swap (char*v[], int i, int j);

if (left >=right)
return ;
swap(v, left, (left+right)/2);
last = left;
for (i = left+1; i<=right; i++)
if ( strcmp(v, v
)<0)
swap (v, ++last, i);
swap(v, left, last);
quicksort(v, left, last-1);
quicksort(v, last+1, right);
}


void swap(char *v[], int i, int j)
{ char *temp;
temp = v;
v = v[j];
v[j] = temp;
}


void forget_word(char *word)
{
int i, wordnum;
void * ptr;

wordnum = binsearch(word, dictionary, num_words);
if (wordnum != -1)
{
num_words--;
for (i = wordnum; i < num_words; i++ )
dictionary = dictionary[i+1];
free(dictionary[wordnum]);
dictionary[num_words+1] = NULL;
}
}



void forget_all(void)
{
int i ;
for (i=0; i < num_words; i++)
{
free(dictionary);
}
free(dictionary);
max_words = 0;
num_words = 0;

}



int read_dict(char *file)
{
char *input;
FILE *fp;
char c;
int i = 0;
int result;
char word[MAXLENGTH];


if( (fp=fopen(file, &quot;r&quot;)) == NULL)
return SPELL_NOTFOUND;

forget_all();
while( c= (getc(fp))!=EOF)
{

if (isalnum(c) == 0)
{
if(strlen(word) != 0)
{
word = '\0';
i = 0;
if ( (result = add_word(word)) == SPELL_NOMEMORY || result == SPELL_FAILURE )
{
fclose(fp);
return result;
}
}
}
else
word[i++] = c;
}
fclose(fp);
return SPELL_OKAY;

}


int check_file(char *file, void (*badword)(char *))
{ char *input;
FILE *fp;
char c;
int i = 0;
int result;
char word[MAXLENGTH];


if( (fp=fopen(file, &quot;r&quot;)) == NULL)
return SPELL_NOTFOUND;


while( c= (getc(fp))!=EOF)
{

if (isalnum(c) == 0)
{
word = '\0';
i = 0;
if (check_word(word) == SPELL_INCORRECT)
{
if (badword != NULL)
badword(word);
fclose(fp);
return SPELL_INCORRECT;
}
else if ( result=check_word(word) == SPELL_FAILURE)
{
fclose(fp);
return SPELL_FAILURE;
}
}
else
word[i++] = c;

}
fclose(fp);
return SPELL_OKAY;

}
 
You seem to have your pointer allocations messed up.

By my count, line 63 reads:
[tt] temp = (char *)realloc(dictionary, new_size * sizeof(char) );[/tt]
and line 66 reads:
[tt] dictionary = temp;[/tt]


You are allocating type char * to temp. But temp and dictionary are defined to be an array of char*, or in c-language terms, char **.

Also, you initialize both temp and dictionary to &quot;{NULL}&quot;. Shouldn't that be &quot;NULL&quot;?
______________________________________________________________________
Did you know?
The quality of our answers is directly proportional to the number of stars you vote?
 
hey try changing that

temp = (char *)realloc(dictionary,new_size * sizeof(char));

to
temp = (char **)realloc(dictionary,new_size * sizeof(char));

coz temp is declared as a
char *temp[]

which is similar to a pointer to a pointer.

and usually if u have 2 arrays like

char a[3],b[3];

a = b is not allowed.

only strcpy(a,b) is allowed. so check the code

temp = dictionary

also in case the 1st doesnt solve the prob.

luv
Karthik.




LOL A ship is safe in the harbour, but that's not what it is meant for!!! LOL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top