pointers to variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saba
    New Member
    • Oct 2006
    • 16

    #1

    pointers to variable

    Create a program that declares an integer array with five elements. Now store in that integer array five different elements (10, 20, 30, 40, 50) using a pointer variable only.

    Hints

    The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to.

    If A [5] is an array then the expressions A[2] and *(A+2) designate the same thing. Use (*A+2) to store the value in the third element of array.



    Solution
    #include <iostream.h>

    main()
    {

    int A[5]= {10, 20, 30, 40, 50};
    int i;

    int * Aptr = A;

    for( i=0; i<5; i++)
    {
    cout << "(*A+" << i << ")=" << A[i] << '\n';
    }

    }

    is it correct???????? ???????????
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by Saba
    Create a program that declares an integer array with five elements. Now store in that integer array five different elements (10, 20, 30, 40, 50) using a pointer variable only.

    Hints

    The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to.

    If A [5] is an array then the expressions A[2] and *(A+2) designate the same thing. Use (*A+2) to store the value in the third element of array.



    Solution
    Code:
    #include <iostream.h>
    
    main()
    {
    
      int A[5]= {10, 20, 30, 40, 50};
      int i;
    
      int * Aptr = A;
    
      for( i=0; i<5; i++)
       {
         cout << "(*A+" << i << ")=" << A[i] << '\n';
       }
    
    }
    is it correct???????? ???????????
    Take a look back at your problem specification. After you have initialized the array of 5 values, you are asked to store the 5 elements using a pointer variable only. From your code, you use Aptr only once - when you declare it!

    Instead, why not use a pointer from the start? As I'm new to C++ and don't have a book handy, I'm not sure of the syntax for this, but I know you can do it. Then, individually set each value of the array as your hint tells you: by using the statement *(A+i) = num, replacing i with whatever position needed (0-4) and replacing num with the appropriate value.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      int A[5]= {10, 20, 30, 40, 50};

      This is an array initialisation, you are told to only declare an array like this

      int A[5];

      The values stored in the array should be set using your pointer

      int * Aptr = A;

      Comment

      • kenx125
        New Member
        • Oct 2006
        • 7

        #4
        Originally posted by Saba
        Create a program that declares an integer array with five elements. Now store in that integer array five different elements (10, 20, 30, 40, 50) using a pointer variable only.

        Hints

        The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to.

        If A [5] is an array then the expressions A[2] and *(A+2) designate the same thing. Use (*A+2) to store the value in the third element of array.



        Solution
        #include <iostream.h>

        main()
        {

        int A[5]= {10, 20, 30, 40, 50};
        int i;

        int * Aptr = A;

        for( i=0; i<5; i++)
        {
        cout << "(*A+" << i << ")=" << A[i] << '\n';
        }

        }

        is it correct???????? ???????????




        Ok here is the code you require.


        #include <iostream>
        using namespace std;


        int main()
        {
        int Array[5];// Declaring an array to hold 5 intergers.
        int *p, num, i;
        p=Array;


        for(i=0; i<5;i++)
        {

        cout <<"Enter num :"<<(i+1)<<endl ;
        cin>>num;
        *(p+i)=num;
        }

        for(i=0; i<5;i++)// To print contents of array
        {
        cout<<*(p+i) <<" ";

        }

        system("PAUSE") ;
        return 0;
        }

        Comment

        Working...