You must Sign In to post a response.
  • Category: Programming

    Searching of a number from an array using pointer to array in C++

    Want a program code for MSBTE Exam for the subject of arrays, C++? Also looking for a explanation of the code? Find responses from experts of code and its explanation on this Ask Expert page.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2018 examination. This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program to search a number from an array using pointer to array. [ 4 Mark]
  • #153386
    Basically, there is a difference between the variable and pointer variable. Variable is able to hold the value of appropriate data type. But the pointer is a variable which holds the address of another variable. So pointer is variable which is capable to hold the address of appropriate data type variable. There is a slight difference in the declaration of variable and pointer variable.
    Variable declaration is as follows
    data type variable name; e.g. int a;
    Pointer variable declaration is as follows
    data type * pointer variable name;
    e.g int * p;
    here * indicates that the variable is a pointer variable.
    Now array is nothing but the group of similar data items with a common name. Example for declaring int array of five elements is as follows.
    int arr[5];
    here arr is the name of the array, which is having five integers as elements of the array. Array elements are stored in a contiguous memory location. As array starts from zeroth element so array name is nothing but the address of a zeroth element of the array.
    So it is possible to write the statement like p=arr; Here p is a integer pointer variable which is able to hold the address of an integer variable and arr is nothing but the address of the zeroth element of an array. So using this pointer p we can access array elements. When the statement p++ arises, it means p is pointed to next element of an array. The general statement about p++ is that when the pointer is incremented by one it is incremented by the size of the data type it is pointing. So using this technique we can access, process array elements.

    Below is the program which finds the required element from the array using the pointer to an array. Please include and header files in the beginning of the program.

    void main(void)
    int arr[5],num,c=0;
    int * p;
    cout<<"Enter any five numbers\n";
    for(p=arr;p {
    cout<<"\nEntered numbers are as follows\n";
    for(p=arr;p {

    cout<<"\nEnter the number to found out\n";
    for(p=arr;p {
    cout<<"\nYour required number "< c=1;
    cout<<"\nYour required number "< }

  • Sign In to post your comments