![]() ![]() In the above example, firstly we defined our array consisting of 3 rows and 2 columns as float marks There is a new form of for loop which makes iterating over arrays easier. This loop continues till i=7 when the value of *(p+7) i.e. Then p++ increases *p to *(p+1) and thus in the second loop, the value of *(p+1) i.e. So when i=0, the value of *p gets printed. In the for loop in the function, p++ increases the value of p by 1. Since p is the address of the array n in the function 'display', i.e., the address of the first element of the array (n), therefore *p represents the value of n. Now, we passed the pointer of an integer i.e., pointer of array n - 'n' as per the demand of our function 'display'. ![]() Void display(int *p) - This means that the function 'display' is taking a pointer of an integer and not returning any value. In the above example, the address of the array i.e., address of n is passed to the formal parameters of the function. Similarly, if we say that a variable y points to an array n, then it means that the value of 'y' is the address of the first element of the array i.e., n. If not, then first read the topic Pointers and practice some problems from the Practice section.Īs we all know that pointer is a variable whose value is the address of some other variable i.e., if a variable y points to another variable x means that the value of the variable 'y' is the address of 'x'. But before starting, we are assuming that you have gone through Pointers. Now, you will see how we can have pointers to arrays too. Till now, you have seen how to declare and assign values to an array. This means that the memories of all elements of an array are allocated together and are continuous. Thus if the address of the first element of an array of integers is X then the address of the second element will be X+4 (4 is the size of one integer) ) and third will be X+4+4 and so on. 'i' will go up to 9, and so indices of the array ( 0,1,2.,9).Īrray allocates contiguous memory. So 'cin > n ' will be used to input the value from the user for n and so on. Similary in the second iteration, the value of 'i' will be 1 and 'n' will be 'n'. So, i and j goes up to 9 and not 10 ( i> n, the user will be asked to enter the value of n. ![]() The code is simple, i and j starts from 0 because index of an array starts from 0 and goes up to 9 ( for 10 elements ). The above code was just to make you familiar with using loops with an array because you will be doing this many times later. We access the elements of an array by writing array_name. ![]() It is like these are the identities of 6 different elements of the array. įollowing is the pictorial view of the array. Hence, no need to specify the array size because the compiler gets it from. In this case, we are declaring and assigning values to the array at the same time. We can also declare an array by another method. Here 'int n' will allocate space to 6 integers. The compiler determines the size of an array by calculating the number of elements of the array. 6 is necessary because the compiler needs to allocate space to that many integers. Here, 6 is the size of the array i.e., there are 6 elements of array 'n'. So, n means that 'n' is an array of 6 integers. Declaring 50 separate variables will do the job but no programmer would like to do so. Suppose we need to store the marks of 50 students in a class and calculate the average marks. eg.- an array of int will contain only integers, an array of double will contain only doubles, etc. In C++ also, an array is a collection of similar types of data. In simple English, array means collection. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |