Your example is an example of a struct
The array can only hold data of the same type:
student s[5]; // array of 5 students :)
Your example is an example of a struct
The array can only hold data of the same type:
student s[5]; // array of 5 students :)
How are they different? Do you mean they have different types?
Yes, exactly! Different types was what I meant
But that is not true arrays hold variables of the same type
Part 1:
Part 2:
initializing an array involves putting curly brackets and the array content inside separated by commas. Empty braces initializes with 0 for each element.
use the sizeof() function.
int arrayLength(array)
{
int length = sizeof(array)/sizeof(array[0]);
return length;
}
1. What is an array?
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
2. Which index would you use to access the 2nd variable in an array?
1
3. What can you say about the size of an array?
A fixed array (also called a fixed length array or fixed size array ) is an array where the length is known at compile time.
1.How can you initialize an array?
Via use of an initializer list . The following example initializes the array
int prime[5]{ 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
int array[5]{ 0, 1, 2, 3, 4 }; // explicitly define the length of the array
int array[]{ 0, 1, 2, 3, 4 }; // let the initializer list set length of the array
The std::size() function from the header can be used to determine the length of arrays.
C++ does not do any checking to make sure that your indices are valid for the length of your array.
So if you try to insert a value in an element that is outside of the length of your array, you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.
This are examples of two ways to initialize the array with a fixed length. What we want to know is the size of the array in bytes?
Indeed there are helper functions that do that, but how would you determine the size of the array without it?
#include <iostream>
using namespace std;
int main() {
int arr[] = {1,2,3,4,5,6,7,8,9,0};
int arrSize = sizeof(arr)/sizeof(arr[0]);
cout << "The size of the array is: " << arrSize << endl;
return 0;
}
PART 1.
PART 2.
An array is a collection of many variables of the same type (e.g. they’re all int, double etc.), labelled with a single identifier.
To find the Mth index in an array of N elements where M <= N, we use the identifier of that array followed by the M-1 in square brackets.
e.g. prime[1] for the 2nd prime number in int prime[N]{}
Fixed arrays, naturally, have a fixed length (at compile time). This is often not helpful; we may want the caller to input data, and we wouldn’t know how large an array we would need. Fortunately there are also *dynamic arrays, with length that can be set at run time and have their length altered thereafter.
You can initialize an array by inputting the elements of the fixed array using an initializer list.
e.g. int prime[10]{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
If some elements in the array are left uninitialized, then they are initialized to 0 for whatever fundamental type the array is listing
e.g. int prime[6] 2.0, 3.0, 5.0, 7.0 } returns 2.0, 3.0, 5.0, 7.0, 0.0, 0.0
This is known as zero initialization.
You can find the size of an array in bytes using the following:
array size = array length * element size
sizeof(array) = std::size() * sizeof(array[0])
where the std::size() function finds the length of an array. Rearranging for std::size():
std::size() = sizeof(array) / sizeof(array[0])
What is an array?
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
Which index would you use to access the 2nd variable in an array?
array[1]
What can you say about the size of an array?
How many elements (or variables) the array contains.
How can you initialize an array?
int array[5]{ 7, 4, 5 }
How can you get the size of an array in bytes?
sizeof(array)
How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array) / sizeof(array[0]);
What happens when you try to write outside the bounds of the array?
Undefined behavior could occur. resulting in overwriting values of other variables or system crashes.
What is an array?
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
Which index would you use to access the 2nd variable in an array?
Since the first element of an array has the index 0, the answer is 1.
What can you say about the size of an array?
The size of a fixed array must be a compile-time constant, which must be known at compile time. However there are also dynamic arrays in C++, but this kind array is a little more complicated to instantiate.
How can you initialize an array?
You can initialize entire arrays via use of an initializer list in C++. You can also initialize an entire array by using an empty initializer list. In this case all array elements will be set to 0 for integers, to 0.0 for doubles and to empty strings for string arrays.
How can you get the size of an array in bytes?
You can use the sizeof(array)
statement to get the number of bytes used by the array in memory.
How can you use the function sizeof in order to calculate the number of elements in an array?
You can divide the number of bytes used by all elements of the array by the number of bytes used by one element of the array: sizeof(array) / sizeof(array[0])
.
What happens when you try to write outside the bounds of the array?
This would overwrite the area in memory where this element would be, if the array had this much elements. But this could lead to undefined behavior, because the value of another variable stored at this location might be overwritten! Negative indices have the same effect. In this case some important value of another variable in the memory before the array may be affected.
What is an array?
An array is an aggregate data type that lets us access many variables of the same type through a single identifier
Which index would you use to access the 2nd variable in an array?
Index 1
What can you say about the size of an array?
There are two types of array. Fixed and dynamic. Programmers allocate the size of a fixed array at launch.
How can you initialize an array?
One way to initialize an array is one element at a time.
Another is with an iinitializer list.
Int prime [5] {2, 3,5,6,11}; If there are more initializers in the list than the array can hold, the compiler will generate an error. If there are less, the remaining elements are initialized to 0 -called zero initialization.
How can you get the size of an array in bytes?
the size of the entire array is equal to the array’s length multiplied by the size of an element. Put more compactly: array size
How can you use the function size of in order to calculate the number of elements in an array?
sizeof(nameOfArray)/sizeOf(nameOfArray[0])
What happens when you try to write outside the bounds of the array?
Either: The array does not compile,
or the value that is stored outside of the bounce will overwrite another piece of code that could crash the code.
Part 1
Part 2
You can initialize an array using an initializer list.
std::cout << sizeof(array) << ‘\n’; // will print the size of the array multiplied by the size of an int
We can use sizeof to find the number of bytes in the array and divide by the byte size of the data type we’re using, in the case below that’s an int.
int array[]{ 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << sizeof(array) << ‘\n’; // will print the size of the array multiplied by the size of an int
std::cout << sizeof(int) << ‘\n’;
The program will write the value in the memory location that would have been used by the array if it had an index high enough. This means a location in memory possibly used by some other function or program has been corrupted by your program. Opps this is bad.
An array is a variable that stores multiple values
arrayName[1];
Its size is fixed by array length * element size
int array[] = { 1,2, etc };
sizeof(array);
sizeof(array)/sizeof(array[0]);
Throws an error
In most cases this is true, but actually the behaviour is undefined.