Thread: Oy my head hurts. More confusion in this, any help?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    21

    Oy my head hurts. More confusion in this, any help?

    Here's a code I'm reading from a tutorial, and I'm trying to make my own program while learning from this. I try to compile this, and I get errors:

    #include <stdio.h>
    #include <stdlib.h>

    struct person {
    char name [20];
    struct person *next;
    };
    struct person *new;
    struct person *head;
    head = NULL;
    main()
    {
    new = (person*)malloc (sizeof(struct person));
    new->next = head;
    head = new;
    }


    The errors are:

    Error c:\tc\test.c 10: Declaration needs type or storage class
    Error c:\tc\test.c 10: Type mismatch in redeclaration of 'head'
    Error c:\tc\test.c 13: Undefined symbol 'person' in function main
    Error c:\tc\test.c 13: Expression syntax in function main
    *** 4 errors in Compile ***

    Any advice? This code was written by the book I'm reading.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct person
    {
       char name [20];
       struct person *next;
    };
    struct person *newbie;
    struct person *head = NULL;
    
    int main()
    {
       newbie = malloc (sizeof(struct person));
       newbie->next = head;
       head = newbie;
       return 0;
    }
    head = NULL;
    You can't execute this here. It must be inside a function or set it during your declaration as I've done.

    Your typecast has an invalid type. It should be type (struct person*). However, you don't need to typecast malloc's.

    Also don't try to use keywords such as "new" as names for your variables. It will complain if you use a c++ compiler.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    Yeah I figured not to use new as a variable when I was writing mine, but this one was written by the book I'm reading. heh

    So when using structures as data types, you have to tell the compiler that it's a strucure? LIke struct bobdole *pointer, not just bobdole *pointer;?

    Interesting.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    Hey it worked!!! You guys are the bomb diggity!!!

    Wooyah

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    So when using structures as data types, you have to tell the compiler that it's a strucure? LIke struct bobdole *pointer, not just bobdole *pointer;?
    Yes. The type is "struct bobdole", if you would use "bobdole" only, you need the keyword typedef, this enables you to define you're own types. Like:

    typedef struct bobdole bobdole_s;

    Now you can do such like this:

    bobdole_s variable;

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Or declare your structure as a typedef:
    Code:
    typedef struct person
    {
       char name [20];
       struct person *next;
    }PERSON;
    Then you can replace every "struct person" with "PERSON" such as:
    newbie = malloc (sizeof(PERSON));
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  4. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  5. Linked List Help
    By Perverse in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2005, 08:33 AM