Structures within Structures

This is a discussion on Structures within Structures within the C Programming forums, part of the General Programming Boards category; I am having trouble accessing structure members within Structures. I have two structures: Code: struct path {int tour[T]; double overall_cost;}; ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21