[cs13001] Arrays in structures

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Oct 16 23:15:22 EDT 2012


> 
> I can't find any arrays in structure examples or mentions in the
> slide, where can I find them?

There can be two different ways to jointly use structures and arrays.

1. A member variable can be an array. For example

      struct example1 {
           int a;
	   double b[10];
      };


   In this case, if a structure variables are later declared, then
   each of such variables will contain an array as members. For example

      example1  c, d;

   Both variables "c" and "d" have member array "b". You can refer
   to the elements of this array. For example

      c.b[5] == 55;

   assigns 55 to the 6th element of the member array of variable "c".


2. You can declare an array of structure variables. For example, you
   can define a structure:

      struct example2 {
      	   int e;
	   double f;
      };

   and later declare an array:

       example2  g[10]

   The elements of this array are structure variables each of which
   has variables "e" and "f" as members. The following code:

       g[6].e = 66;

   Assigns 66 to the member variable "e" of the 7th element of array "g".

The complexity may be further increased. For example, you can declare
an array of "example1".

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list