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

    Program using C++ to implement the concept of pointer to array


    Preparing for MSBTE C++ Computer Programming subject? Searching for the correct program to implement the concept of a pointer to an array? Find advice from experts on this page for your queries.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Winter 2017 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 which implement the concept of pointer to array. [4 Marks]
  • Answers

    1 Answers found.
  • Basically array is nothing but the group of similar data items which shares a common name. Pointer is a variable which is capable to hold the address of another variable. Internally array is processed by using the concept of pointer. Array has continuous memory to store the elements of an array. First element of an array is indicated by index number 0. e.g. arr[0].
    There is a classic definition of an array which is very suitable for pointer to array. Array name indicates the address of zeroth location of an array. If we have an integer array say int arr[5] and also if we have an integer pointer say *p. Then the statement p=arr; is perfectly all right and as mentioned above arr is nothing but the address of zeroth location of an array named arr and it is stored by an integer pointer p. When pointer is incremented by one like p=p+1; then it get incremented by the size of data type to which it points. So if p hold address say 100 then by increment p =p+1; it holds the address 102. In this way using this concept it is possible to perform any required operation on array. There is a close relation between array and pointer.

    Below is the program which show the implementation of pointer to array.

    Please add iostream.h and conio.h header files before declaration of class array.

    //PROGRAM TO IMPLEMENT CONCEPT OF POINTER TO ARRAY
    #include
    #include
    class array
    {
    private:
    int arr[10],max;
    int *p;
    public:
    void get(void)
    {
    p=arr;
    cout<<"\nEnter any ten integers\n";
    for(p=arr;p {
    cin>>*p;
    }
    p=arr;
    }
    void display(void)
    {
    max=*p;
    for(p=arr;p {
    if(*p>max)
    {
    max=*p;
    }
    }
    p=arr;
    cout<<"\nArray elements are as follows\n";
    for(p=arr;p {
    cout<<*p<<" ";
    }
    cout<<"\nMaximum number among above array elements is "< }
    };
    void main(void)
    {
    class array ob;
    clrscr();
    ob.get();
    clrscr();
    ob.display();
    getch();
    }

    In the above program statement p=arr; is used to hold the base address(address of zeroth element) of an array into pointer. When pointer get incremented in a loop, at a certain point that is when loop get failed, the address hold by pointer is beyond array limit. So the statement like p=arr; is used at multiple times to make pointer p to point to zeroth location of an array.
    Above program accepts ten integer values from user and find the maximum value. Like that we may perform any array related operation using pointer to array.


  • Sign In to post your comments