Array and Functions
Array and Functions
C++ Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store:
int myNum[3] = {10, 20, 30};
In C++, you don't have to specify the size of the array. The compiler is smart
enough to determine the size of the array based on the number of inserted values:
You access an array element by referring to the index number inside square
brackets [].
Cout<<myNum[1]<<endl; //20
You access an array element by referring to the index number inside square
brackets [].
Using loop;;;;;;
The size of int[] is basically cuounting the number of elements inside that array. To get this we can use
the sizeof() operator. If the array name is passed inside the sizeof(), then it will return total size of
memory blocks that are occupied by the array. Now if we divide it by the size of each element, then we
can get number of elements.
1. To find out the size of an array in C++ we can use multiple techniques. Using sizeof
Operator to find Size of Array in C++.
2. Using pointer to calculate size of array. (will cover after studying pointers)
3. Using end () and begin () functions. Using std::array container size () function.
1-
int data[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 91, 82, 73, 64 };
cout << "Memory occupied by data[]: " << sizeof(data) << endl;
cout << "Size of data[] array: " << sizeof(data) / sizeof(data[0]) <<
endl;
int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(int);
cout << getArrayLength;
The function len () is one of Python ’ s built −¿ functions . It returns the length of an object. For
example, it can return the number of items in a list.
int data[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 91, 82, 73, 64 };
cout << "Memory occupied by data[]: " << sizeof(data) <<endl;
cout << "Size of data[] array: " << sizeof(data) / sizeof(data[0]) <<
endl;
len = sizeof(data) / sizeof(data[0]);
showArray(data);
system("pause");
}//end of main
another method
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < sizeof(myNumbers) / sizeof(int); i++) {
cout << myNumbers[i] << "\n";
}
Actually, you do not place the null character at the end of a string constant. The C++ compiler
automatically places the '\0' at the end of the string when it initializes the array. Let us try to print
above-mentioned string –
#include <iostream>
int main () {
return 0;
}
1
strcpy(s1, s2);
Copies string s2 into string s1.
2
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3
strlen(s1);
Returns the length of string s1.
4
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5
strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6
strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
Learning:
return 0;
}
When the above code is compiled and executed, it produces the following result −
Address of var1 variable: 0xbfebd5c0 //it may differ
Address of var2 variable: 0xbfebd5b6 //it may differ
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is
the same, a long hexadecimal number that represents a memory address. The only difference
between pointers of different data types is the data type of the variable or constant that the
pointer points to.
Note
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable
return 0;
}
When the above code is compiled and executed, it produces result something as follows −
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac//it may differ
Value of *ip variable: 20
Pointers in C++
Pointers have many but easy concepts and they are very important to C++ programming. There
are following few important pointer concepts which should be clear to a C++ programmer −
1 Null Pointers
C++ supports null pointer, which is a constant with a value of zero defined in several standard
libraries.
2 Pointer Arithmetic
There are four arithmetic operators that can be used on pointers: ++, --, +, -
3 Pointers vs Arrays
There is a close relationship between pointers and arrays.
4 Array of Pointers
You can define arrays to hold a number of pointers.
5 Pointer to Pointer
C++ allows you to have pointer on a pointer and so on.
5- Pointer to Pointer
A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name. For example, following is the declaration to declare a
pointer to a pointer of type int −
int **var;
When a target value is indirectly pointed to by a pointer to a pointer, accessing that value
requires that the asterisk operator be applied twice, as is shown below in the example −
#include <iostream>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of var :3000
Value available at *ptr :3000
Value available at **pptr :3000
Learning:
pptr = &ptr;
when pointer is assigned to pointer to pointer (do not use word double pointer)
int main () {
unsigned long sec;
getSeconds( &sec );
return 0;
}
void getSeconds(unsigned long *par) {
// get the current number of seconds
*par = time( NULL );
return;
}
The function which can accept a pointer, can also accept an array as shown in the following example –
#include <iostream>
using namespace std;
// function declaration:
double getAverage(int *arr, int size);
int main () {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
return 0;
}
return avg;
}
When the above code is compiled together and executed, it produces the following result −
Average value is: 214.4
7-Return Pointer from Functions in C++
C++ allows a function to return a pointer to local variable, static variable and dynamically allocated
memory as well.
As we have seen in last chapter how C++ allows to return an array from a function, similar way C++
allows you to return a pointer from a function. To do so, you would have to declare a function returning a
pointer as in the following example –
int * myFunction() {
.
.
.
}
Second point to remember is that, it is not good idea to return the address of a local variable to
outside of the function, so you would have to define the local variable as static variable.
Now, consider the following function, which will generate 10 random numbers and return them
using an array name which represents a pointer i.e., address of first array element.
Simple example
int * getArray(int [],int); ////Return Pointer from
Functions in C++
main()
cout << "Address of"<<i<<" index: " << &ptr[i] << "
Value at " << i << "index" << *ptr << endl;
ptr++;
}
}
return y;