By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

problem with linked list

P: n/a
Hello!

I am an newbie, and I am a litle stuck here. Could anyone please tell me
what I am doing wrong?
Here is my code:

#include <stdio>
#include <stdlib>

struct node {
char name[30];
struct node *next;
};
struct node *Head, *new_ptr;

void main(){
int length,counter;
char value[30];
printf("How many nodes: ");
scanf("%d",&length);
Head = NULL;
for(counter=0; counter<length; counter++){
new_ptr = malloc(sizeof(struct node));
if(new_ptr==NULL){
printf("not enough memory\n");
break;
}
printf("Enter the name (max 30 chr) for node %d: ",counter);
scanf("%s",&value);
new_ptr->name = value;
new_ptr->next = Head;
Head = new_ptr;
}
}
I am using the Borland c compiler an here is the error message:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\c-files\14-1-04\struct4.c:
Error E2277 C:\c-files\14-1-04\struct4.c 24: Lvalue required in function
main
*** 1 errors in Compile ***

I checked up this lvalue in the comp.lang.c FAQ, but i didn't understand
what concrete i am doing wrong?!

Thanks in advanced
David
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
David Tandberg-Johansen <du***@mix.hive.no> scribbled the following:
Hello! I am an newbie, and I am a litle stuck here. Could anyone please tell me
what I am doing wrong?
Here is my code: #include <stdio>
#include <stdlib>
These should end in .h
struct node {
char name[30];
struct node *next;
};
struct node *Head, *new_ptr;
Why are these global? You never use them outside main().
void main(){
Illegal form of main(). Use int main(void).
int length,counter;
char value[30];
printf("How many nodes: ");
scanf("%d",&length);
Head = NULL;
for(counter=0; counter<length; counter++){
new_ptr = malloc(sizeof(struct node));
if(new_ptr==NULL){
printf("not enough memory\n");
break;
}
printf("Enter the name (max 30 chr) for node %d: ",counter);
scanf("%s",&value);
new_ptr->name = value;
This is what is wrong. You can't assign to array variables like that.
Use strcpy(), and remember to #include <string.h>.
new_ptr->next = Head;
Head = new_ptr;
}
} I am using the Borland c compiler an here is the error message:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\c-files\14-1-04\struct4.c:
Error E2277 C:\c-files\14-1-04\struct4.c 24: Lvalue required in function
main
*** 1 errors in Compile *** I checked up this lvalue in the comp.lang.c FAQ, but i didn't understand
what concrete i am doing wrong?!


What concrete thing you are doing wrong is the new_ptr->name = value
thing. new_ptr->name is not an assignable lvalue.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
- Andy Capp
Nov 14 '05 #2

P: n/a
nrk
David Tandberg-Johansen wrote:
Hello!

I am an newbie, and I am a litle stuck here. Could anyone please tell me
what I am doing wrong?
Here is my code:

#include <stdio>
#include <stdlib>

Smells strongly like another language close to C. You probably want these
to be:
#include <stdio.h>
#include <stdlib.h>
struct node {
char name[30];
name is an array of 30 characters.
struct node *next;
};
struct node *Head, *new_ptr;

void main(){
main returns int in C.
int main()
int length,counter;
char value[30];
printf("How many nodes: ");
You need to flush the output stream to ensure that this prompt appears to a
user:
fflush(stdout);
scanf("%d",&length);
Need to check return value of scanf to ensure no errors were encountered,
and that length was initialized to a user supplied value.
Head = NULL;
for(counter=0; counter<length; counter++){
new_ptr = malloc(sizeof(struct node));
CLC preferred way of doing this:
new_ptr = malloc(sizeof *new_ptr);
This avoids hard-coding the type of new_ptr unnecessarily.
if(new_ptr==NULL){
printf("not enough memory\n");
break;
}
printf("Enter the name (max 30 chr) for node %d: ",counter);
Again, flush the output stream so that the prompt will appear to the user:
fflush(stdout);
scanf("%s",&value);
Ouch!! What you want is:
scanf("%29s", value);

Note the absence of the & in front of value. Also, since there's a maximum
of 29 characters, we can enforce that as well.
new_ptr->name = value;
You cannot directly assign to an array in C like this (and this is what your
compiler is telling you). In this case, since your arrays are strings, you
could use strcpy instead:
strcpy(newptr->name, value);
new_ptr->next = Head;
Head = new_ptr;
}
Remember, main returns int.
return 0; }

You also have logic problems with your linked list. You don't retain the
actual starting node of the list as Head.

-nrk.

I am using the Borland c compiler an here is the error message:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\c-files\14-1-04\struct4.c:
Error E2277 C:\c-files\14-1-04\struct4.c 24: Lvalue required in function
main
*** 1 errors in Compile ***

I checked up this lvalue in the comp.lang.c FAQ, but i didn't understand
what concrete i am doing wrong?!

Thanks in advanced
David


Nov 14 '05 #3

P: n/a
nrk
nrk wrote:

<snip>

You also have logic problems with your linked list. You don't retain the
actual starting node of the list as Head.


Sorry, got carried away there. No such problem in your code, and you will
retain the right Head value of your list.

-nrk.

<snip>
Nov 14 '05 #4

P: n/a
On Wed, 14 Jan 2004 09:08:57 GMT, nrk <ra*********@devnull.verizon.net>
wrote:
David Tandberg-Johansen wrote:
Hello!

I am an newbie, and I am a litle stuck here. Could anyone please tell me
what I am doing wrong?
Here is my code:

#include <stdio>
#include <stdlib>


Smells strongly like another language close to C. You probably want
these
to be:
#include <stdio.h>
#include <stdlib.h>
struct node {
char name[30];


name is an array of 30 characters.
struct node *next;
};
struct node *Head, *new_ptr;

void main(){


main returns int in C.
int main()
int length,counter;
char value[30];
printf("How many nodes: ");


You need to flush the output stream to ensure that this prompt appears
to a
user:
fflush(stdout);
scanf("%d",&length);

doing fflush(stdout) before scanf is not necessary, as the pending output
to terminal devices(output stream) is written automatically whenever an
input stream that refers to a terminal device is read.

[...]
Nov 14 '05 #5

P: n/a
nrk
A. J. Mohan Rao wrote:
On Wed, 14 Jan 2004 09:08:57 GMT, nrk <ra*********@devnull.verizon.net>
wrote:
David Tandberg-Johansen wrote:
Hello!

I am an newbie, and I am a litle stuck here. Could anyone please tell me
what I am doing wrong?
Here is my code:

#include <stdio>
#include <stdlib>


Smells strongly like another language close to C. You probably want
these
to be:
#include <stdio.h>
#include <stdlib.h>
struct node {
char name[30];


name is an array of 30 characters.
struct node *next;
};
struct node *Head, *new_ptr;

void main(){


main returns int in C.
int main()
int length,counter;
char value[30];
printf("How many nodes: ");


You need to flush the output stream to ensure that this prompt appears
to a
user:
fflush(stdout);
scanf("%d",&length);

doing fflush(stdout) before scanf is not necessary, as the pending output
to terminal devices(output stream) is written automatically whenever an
input stream that refers to a terminal device is read.

[...]


Chapter & Verse please.

-nrk.
Nov 14 '05 #6

P: n/a
nrk <ra*********@devnull.verizon.net> wrote:
A. J. Mohan Rao wrote:
On Wed, 14 Jan 2004 09:08:57 GMT, nrk <ra*********@devnull.verizon.net>
wrote:
David Tandberg-Johansen wrote:

Hello!

I am an newbie, and I am a litle stuck here. Could anyone please tell me
what I am doing wrong?
Here is my code:

#include <stdio>
#include <stdlib>
Smells strongly like another language close to C. You probably want
these
to be:
#include <stdio.h>
#include <stdlib.h>

struct node {
char name[30];

name is an array of 30 characters.

struct node *next;
};
struct node *Head, *new_ptr;

void main(){

main returns int in C.
int main()

int length,counter;
char value[30];
printf("How many nodes: ");

You need to flush the output stream to ensure that this prompt appears
to a
user:
fflush(stdout);

scanf("%d",&length);

doing fflush(stdout) before scanf is not necessary, as the pending output
to terminal devices(output stream) is written automatically whenever an
input stream that refers to a terminal device is read.

[...]

Chapter & Verse please.


The closest is the following wording from 7.19.3:

[#3] [...] Furthermore, characters are intended to be
transmitted as a block to the host environment when a buffer
is filled, when input is requested on an unbuffered stream,
or when input is requested on a line buffered stream that
requires the transmission of characters from the host
environment. Support for these characteristics is
implementation-defined, and may be affected via the setbuf
and setvbuf functions.

So the intent of this behavior is indeed there, but it is not
strictly enforced.
--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #7

P: n/a
"A. J. Mohan Rao" wrote:

.... snip ...

doing fflush(stdout) before scanf is not necessary, as the pending
output to terminal devices(output stream) is written automatically
whenever an input stream that refers to a terminal device is read.


Chapter and verse please. While many implementations may do so, I
don't believe it is part of the standard. In addition, what says
that the input and output streams are part of an interactive pair?

These are the sort of bugs that make the difference between good
and poor software.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.