what is problem with this code .Please expain?

help

#1

#include

using namespace std;

int main()
{
    int *p;
    for(int i=0;i<10;i++)
    {
        cout<<"enter value for element ";
        cin>>*p;
        p++;
    }
    for(int i=0;i<10;i++)
    {
        --p;
        cout<<"

enter value for element "<<*p;
}
return 0;
}


#2

The only problem with your code is that you have just declared a pointer, but that pointer is not pointing to any location i.e it has not been allocated any space.

If you want to use the pointer to be used in place of an array, you have to allocate it enough space on the heap.

In the above case

//size of array 10 *p = (int *)malloc(10*sizeof(int)) // c implementation, Don't forget to include cstdlib *p = new int[10] // c++ implementation

This would solve your problem.

P.S.
Casting the void pointer returned by malloc is a bad practice and is not recommended.