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?
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?
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.
An array is made to keep one or more variables of a certain type.
will declare an array which can hold 5 integers.Code:int a[5];
gives an example of a struct point and an array myPoints which can hold 5 points and which is initialized with 5 points.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}};
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:
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.Code:const int ARRAYSIZE = 10; int array[ARRAYSIZE]; for (int i = 0; i < ARRAYSIZE; ++i) { *(array + i) = 3; }
it could just as easily (and probably more clearly) be written as follows:
in the second case, we're taking advantage of the language's built-in array index operator.Code:const int ARRAYSIZE = 10; int array[ARRAYSIZE]; for (int i = 0; i < ARRAYSIZE; ++i) { array[i] = 3; }
Not exactly. It's like a pointer in most ways. You cannot however do this:http://c-faq.com/aryptr/index.htmlCode:int array[ ARRAYSIZE ]; array++; /* no */ *array = 5; /* no */
Quzah.
Hope is the first step on the road to disappointment.