Re: problem with linked list
From: nrk (ram_nrk2000_at_devnull.verizon.net)
Date: 01/14/04
- Next message: nrk: "Re: Code Review: strncpy"
- Previous message: Vijay Kumar R Zanvar: "Re: Code Review: strncpy"
- In reply to: David Tandberg-Johansen: "problem with linked list"
- Next in thread: nrk: "Re: problem with linked list"
- Reply: nrk: "Re: problem with linked list"
- Reply: A. J. Mohan Rao: "Re: problem with linked list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 14 Jan 2004 09:08:57 GMT
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
- Next message: nrk: "Re: Code Review: strncpy"
- Previous message: Vijay Kumar R Zanvar: "Re: Code Review: strncpy"
- In reply to: David Tandberg-Johansen: "problem with linked list"
- Next in thread: nrk: "Re: problem with linked list"
- Reply: nrk: "Re: problem with linked list"
- Reply: A. J. Mohan Rao: "Re: problem with linked list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|