Thread: C array to hold multiple structs?

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    C array to hold multiple structs?

    I am not sure if this is possible but I am looking to create an array data type in which to hold a struct.

    Is this even possible? If so can you show me an example or perhaps a resource to learn more about it?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It works like any other array.
    Code:
    struct s
    {
        int a;
        char b;
    };
    
    struct s array[10];
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    An array is made to keep one or more variables of a certain type.

    Code:
    int a[5];
    will declare an array which can hold 5 integers.

    Code:
    struct point{
    	double x;
    	double y;
    }; 
    typedef struct point point;
    
    point myPoints[5] = {{1.0, 2.0}, {1.0, 4.0}, {2.0,3.0}, {1.0, 3.0}, {3.0, 7.0}};
    gives an example of a struct point and an array myPoints which can hold 5 points and which is initialized with 5 points.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    An array in of itself is not a data type but more of a container. A structured way of organizing your memory allocation of like items, either ints, chars or in your case many structures of the same declaration.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by slingerland3g View Post
    An array in of itself is not a data type but more of a container. A structured way of organizing your memory allocation of like items, either ints, chars or in your case many structures of the same declaration.
    Additionally, the name of the array is a pointer to the first element of the array. Take the following code, for example:
    Code:
    const int ARRAYSIZE = 10;
    int array[ARRAYSIZE];
    for (int i = 0; i < ARRAYSIZE; ++i)
    {
      *(array + i) = 3;
    }
    this will set all of the values in the array to 3. when you add an integer value to a pointer in C or C++, behind the scenes, the system multiplies that integer by the size of the type pointed to by the array, and adds that to the base address of the array, giving you, in the case of my example, the element at index i.

    it could just as easily (and probably more clearly) be written as follows:
    Code:
    const int ARRAYSIZE = 10;
    int array[ARRAYSIZE];
    for (int i = 0; i < ARRAYSIZE; ++i)
    {
      array[i] = 3;
    }
    in the second case, we're taking advantage of the language's built-in array index operator.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elkvis View Post
    Additionally, the name of the array is a pointer to the first element of the array.
    Not exactly. It's like a pointer in most ways. You cannot however do this:
    Code:
    int array[ ARRAYSIZE ];
    array++; /* no */
    *array = 5; /* no */
    http://c-faq.com/aryptr/index.html


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  2. Replies: 19
    Last Post: 12-17-2007, 02:57 AM
  3. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array of structs
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 04-18-2002, 01:51 PM