Thread: Structure..

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    Structure..

    I have this array

    Code:
    int array[3][4] =    
        {{1,2,3},
         {4,5,6},
         {7,8,9},
         {10,11,12}};
    
    And and structure like this:
    
    struct database
    {
      int x_pos;
      int y_pos;
      int type_;
      int money;
      int credit;
    };
    If I want to access item 1,2 in the array I do like this:
    number_ = array[1] [2]

    But since the array only can have 1 information item in it I have an problem.

    I want an structure that I can access in an similar way as the array.

    So, basically I want an [3] [4] array of the structure.

    And I have no idea on how to do this, can you help me?

    So when I for example want the information at location [1] [2] in the structure-array I could get the type_, money, credit etc from it.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    database my_database[3][4];

    Here's an example:

    Code:
    #include <iostream>
    using namespace std;
    
    struct database
    {
    	int number;
    	double test;
    };
    
    int main()
    {
    
    	database my_database[3][4]; //Declare an array of database structs
    	my_database[0][0].number = 10;
    	my_database[0][0].test = 45.32;
    
    	cout<<my_database[0][0].number<<endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM