Thread: Structures within Structures

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Question Structures within Structures

    I am having trouble accessing structure members within Structures.

    I have two structures:
    Code:
    struct path {int tour[T]; double overall_cost;};
    
    struct ant {int numb; struct path; int rank;};
    When i use a pointer to ant to access the array pointer inside the structure path I use the following:
    Code:
    ant_ptr->path->tour[i]=j;
    However this doesn't seem to work, Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The second -> should be just .
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    struct path {int tour[T]; double overall_cost;};
    
    struct ant {int numb; struct path; int rank;};
    You still need to declare what will hold path (ie a variable), something like:

    Code:
    struct path {
        int tour[T];
        double overall_cost;
    };
    
    struct ant {
        int numb;
        struct path p;
        int rank;
    };
    And access it through,
    Code:
    ant_ptr->p.tour[i] = j;
    Consider browsing through http://www.cprogramming.com/tutorial/lesson7.html if you haven't done so already.
    Last edited by zacs7; 08-27-2007 at 12:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM