Re: problem with linked list

From: nrk (ram_nrk2000_at_devnull.verizon.net)
Date: 01/14/04


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



Relevant Pages

  • Re: Error handling library
    ... which lets the compiler catch out-of-range usage. ... and assuming that a higher int means "more dangerous error" ... with a comment warning that one is used as an index into the array ... languages while running (an array of languages, ...
    (comp.lang.c)
  • Re: Maximum char array size?
    ... versus declaring an array at compile time (other than the need ... I did not get anywhere before I declared the new type ARRAY2. ... and DMC (Digital Mars Compiler) on WinXp. ... void PrintArray(char *name, int *array, int ncol, int nrow) ...
    (comp.lang.c)
  • Re: On VLAs and incomplete types
    ... I don't understand how a variable is handled by the compiler in ... declaring a VLA. ... an array must be declared with an ... int x; ...
    (comp.lang.c)
  • Re: operator function
    ... >> of them have anything to do with what the error message seems ... Setting (int h, int m, int s, int TT) ... Now the compiler knows, that it is safe to use that function on a const ... >> operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: max size of ptr array
    ... you'll get a compiler error. ... number you are allowed type in the brackets is exactly 536870911. ... int main{ ... And since an int requires 4 "bytes" plus the fact that your array is ...
    (microsoft.public.vc.language)