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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with compile error. 1

Status
Not open for further replies.

anatazi

Programmer
Jun 24, 2002
33
US
Hi all,
Can someone please tell me what is wrong with the code below.i get a " "curr->name" is not an lvalue,but occurs in a context that requires one.curr->name=list1;" message when i compile it.
Why is that?
Thank you.

#include<string.h>
#include <stdio.h>
#define CHARS 30
#define NAMES 20
main()
{
struct NODE {
char name[CHARS];
struct NODE *next;
};
typedef struct NODE Node;

int count= -1;
int i,j,x;
char list1[NAMES][CHARS],list2[NAMES][CHARS];
FILE *fp;
Node* head;
Node* curr;
head=(Node*)malloc(sizeof(Node));
curr=head;

fp=fopen(&quot;merge.dat&quot;,&quot;r&quot;);
while (!feof(fp)) {
count++;
fscanf(fp,&quot;%s %s &quot;,list1[count],list2[count]);
}
fclose(fp);

while(i<count && j<count)
{
curr->next=(Node*)malloc(sizeof(Node));
curr=curr->next;
if(strcmp(list1,list2[j])<0)
{
curr->name=list1;
j++;
}
else
{
curr->name=list2[j];
i++;
}

}

if (i=count-1)
for(x=j;x<count;x++)
{
curr->next=(Node*)malloc(sizeof(Node));
curr=curr->next;
curr->name=list2[x];
}
else
for(x=i;x<count;x++)
{
curr->next=(Node*)malloc(sizeof(Node));
curr=curr->next;
curr->name=list1[x];
}
}
 
You cannot use the '=' operator on statically allocated strings, like the name[CHARS] in your structure. Even if you dynamically allocate memory for the structure, the character array is statically allocated.
Use strcpy instead.

In General, it is a bad practice to use the assignement operator on strings, even if they are pointers. Remember that an statically declared array is a pointer too, which is statically allocated on the heap, and doing an assignement on it would imply a change of pointer, which is regarded as illegal.

Ex:

Code:
char array[100];
...
array = &quot;100 characters....&quot;; /* invalid; */
strcpy(array,&quot;100 characters...&quot;); /* !!Valid */
HTH. [b][red]Nosferatu[/red][/b]
We are what we eat...
There's no such thing as free meal...
[i]once stated:[/i] methane@personal.ro
 
Hi,
but why am I getting a memory fault now?
Thanks
 
Where do you get a memory fault? [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Hi again, I made the changes with strcmp and the program compiles with no error but when I run it I get memory fault.
Thanks
 
Well, one thing is that you use i and j without initializing them to a value. This means that they can contain absolutely anything. When you use them to index an array in the while-loop you will probably run around in memory which is not valid and you will get a crash.

Initialize them to 0 when you declare them and at least that problem will be solved.

It is generally a good thing to _always_ initialize variables to 0 when you declare them, even if you set them to something valid right, just so that things like this does not happen.

Hope it works =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top