problem with linked list

  • Thread starter David Tandberg-Johansen
  • Start date
D

David Tandberg-Johansen

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
 
J

Joona I Palaste

David Tandberg-Johansen said:
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() said:
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 ([email protected]) ------------- 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
 
N

nrk

David said:
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 said:
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.
 
N

nrk

nrk wrote:

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>
 
A

A. J. Mohan Rao

David said:
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 said:
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.

[...]
 
N

nrk

A. J. Mohan Rao said:
David said:
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 said:
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.
 
A

Alex Monjushko

nrk said:
A. J. Mohan Rao 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.
 
C

CBFalconer

A. J. Mohan Rao said:
.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top