Thread: FAQ struct question... (C)

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    struct question...

    Hello.

    Anyway, will someone tell me what this piece of
    code does?

    Code:
    struct dastructure {
      int abcd;
      struct dastructure *next_abc;
    }
    That part that confuses me is the struct dastructure
    *next_abc
    statement. What exactly is that doing?
    I know that *next_abc is a pointer, but why is the
    struct keyword used again?

    Thanks,
    static.
    Staying away from General.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    When using structires, you have to use the keyword struct every time you refer to a structure class. It's kinda an annoying feature of the language, *shrug*. So that's why you need to use the struct keyword within the struct.

    And for explaining why there's a pointer in the struct... that kinda struct that you posted looks to be a linked list node. Consider that we wanted to keep a set of integers, using the abcd field of this structure. Each structure would represent one integer...

    The first struct would contain the first value, and it's pointer would point to the second struct.
    The second struct would contain the second value, and it's pointer would point to the third struct.
    ...
    The last struct would contain the last value, and it's pointer would be NULL.

    So from the first struct, we can reach any value in the list by going from struct to struct. Of course, we'd have to keep a pointer to the first struct as well.


    It's the kind of thing where pictures help, but those aren't really available to me. Most any textbook should be able to illustrate the idea, and all the neat things one can do with linked lists that were impossible with arrays.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The reason is that a structure in C does not define a new data type, (this is a difference between C and C++ because in C++ it does). The name you give after a struct keyword is called it's tag, the tag by itself is not a type name. Thus when you want to create a variable that is a struct in C you must say it is a struct, then specify the tag to identify which kind of struct. The struct keyword is ignored by a C++ compiler when declaring variables or pointers to a struct.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM