Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Bilal Junaidy
1,775 PointsArrays
Could someone please explain me the use of the following
- multi dimensional arrays
- Arrays on strings
- Arrays of union
- Arrays of structure here is a link to the gnu c manual https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Arrays
5 Answers
Stone Preston
42,016 Points(1) a multidimensional array is basically an array of arrays, or arrays nested inside one another.
int two_dimensions[2][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10} };
the above line creates a multidimensional array ( in thise case its 2d). the items of the array are arrays themselves. you access particular items using the same notation as regular arrays, but you use multiple sets of [ ]
two_dimensions[0][3] accesses the first item ( 0 index ) which would be {1, 2, 3, 4, 5} and then the 4th item (3 index) inside that which is 4.
so the value of two_dimensions[0][3] is 4.
and the value of two_dimensions[1][0] is 6
(2) you can use an array to store a string which is just a collection of characters. so if I wanted to store the string "stone" I could use
char stringName[5] = "stone";
or
char stringName[] = { 's', 't', 'o', 'n', 'e', '\0' };
when using the latter method of declaring strings, you use the null character (\0) to specify the end of the string.
(3) you create an array of unions just as you do an array of any other type
first define your union
union numbers
{
int i;
float f;
};
the above code creates a union called numbers with a member of type int called i and of type float called f
you can declare an array of that type like so:
union numbers number_array[3];
and assign its elements values
number_array[0].i = 2;
number_array[0].f =1.2;
(4) an array of structs is similar:
first define your struct
struct point
{
int x, y;
};
the above code defines a struct with 2 int members, x and y
then declare your array
struct point point_array [3];
point_array[0].x = 2;
point_array[0].y = 3;
you could also define and declare your array of points in one line
struct point point_array [3] = { {2}, {4, 5}, {6, 7} };
the value point_array[1].x is 4. point_array[2].y is 7. point_array[1] is the array {4,5} etc etc
The link you posted does a really good job of explaining everything, did you read it? I pretty much just added a bit more explanation to what was mentioned in the link
Bilal Junaidy
1,775 PointsI did read it but I am new to programming so maybe thats why I had trouble. Thanks for the help, I really appreciate it
Bilal Junaidy
1,775 Pointscould you explain why the below code is giving an error char my_name_using_the_other_method [] = {'b' 'i' 'l' 'a' 'l' '\0'}; printf("my_name_using_the_other_method %c\n", my_name_using_the_other_method[2]);
I mean according to my understanding the answer should be the letter l but xocde is giving an error
Stone Preston
42,016 Pointsyou need to separate the characters in the array with commas
Bilal Junaidy
1,775 Pointsoh thanks again
Bilal Junaidy
1,775 PointsI didnt understand the following part of your answer
- Array of Union
union numbers number_array[3]; what does the above code do. I mean you have writtebn down that it a way to declare your array but what does that mean. and also how does the following code assigns value to its element? number_array[0].i = 2; number_array[0].f =1.2;
- Array of structure What do the below two lines of code imply.
struct point point_array [3]; point_array[0].x = 2; point_array[0].y = 3;
struct point point_array [3] = { {2}, {4, 5}, {6, 7} };