# What is Array?
# As you know that a variable can only store value one value at a time.
# sometimes you want to create a lot of variable with the same type, So do you going to create each of them or Why do not you look at array? What if offers for you?
# An Array is a series of elements of the same type placed in contiguous memory locations that can be easily accessed whenever wanted.
# Why Array was implemented ?
# Instead of creating a lot of variables that will take more memory that will be better if we can declare an array in such a manner that can hold our all variables contents just using a single identifier name.
# How to declare an Array?
# How to declare an Array using C++?
# Let me declare an Array, so that i can put content there.
type array_name[ array_size ];
int foo[5];
# We just declare an static array that can hold 5 integer values.
# How to declare and initialize an Array?
# How to initialize Array using C++?
int foo [5] = { 16, 2, 77, 40, 12071 };
# Explicity initialize all elements of array
int bar [5] = { 10, 11, 12};
# remaining Array elements will be initialize to ZERO.
int bar [5] = {};
# all Array elements will be initialize to ZERO
int foo[] = { 16, 7, 25, 40, 12909};
# Compiler will assume that length of Array will be number of initialized elements.
# New Syntax:- both are same
int foo[] = { 10, 11, 12, 13, 14 };
int foo[] { 10, 11, 12, 13, 14 };
# How to access values of an Array?
# You need Index number of array element that you want to access.
# Numbering starts from ZERO.
int foo [3] = { 10, 11, 12};
# means :-
foo[0] = 10;
foo[1] = 11;
foo[2] = 12;
# What about Multidimensional Array?
# Multidimensional Arrays are nothing more than Array of Arrays.
# Means, an Array element will be an Array itself.
int jimmy [3][5];
# just declared a multidimensional array.
[ 0x0 ] [ 0x1 ] [ 0x2 ] [ 0x3 ] [ 0x4 ]
[ 1x0 ] [ 1x1 ] [ 1x2 ] [ 1x3 ] [ 1x4 ]
[ 2x0 ] [ 2x1 ] [ 2x2 ] [ 2x3 ] [ 2x4 ]
# Alternative Syntax for accessing Array elements:-
## Both are same:-
int jimmy [3][5];
int jimmy [15]; // 3 * 5 == 15
# What if i want to pass my Array as a function parameter?
# To accept an array as parameter for a function, the parameters can be declared as the array type, but with empty array brackets, omitting the actual size of the array.
int procedure ( int arg[] );
# This function expects an Array of type Int.
# You can call this function as
int myarray[40];
procedure( myarray );
# Is there any standard library to play with Array?
# In C++ array are inherited from C language.
# C++ has an alternative array type as a standard container. it is a type template ( a Class template) defined in header <array>.
# If you are using standard C++ array container then they give you some powerful methods that you can use with them.
# References :-
http://www.cplusplus.com/doc/tutorial/arrays/
No comments :
Post a Comment