0

I wanted to pass an array pointer to a function and show content of that array from that function.I did this in below way but when i run the code the whole program is crashing.Please help me or give me idea how to solve this problem...

  int main()
    {
        int size,i;
        cout<<"Please enter the size of the array";
        cin>>size;
        int *array_=new int [size];
        cout<<"Please enter all elements of the array";
        for(i=0;i<size;i++){
            cin>>array_[i];
        }
        insertion(&array_,size);
        return 0;
    }
    void insertion(int *array_[],int size){
        int i;
        for(i=0;i<size;i++){
            cout<<*array_[i];
        }
    }
5
  • You are passing the address of a pointer variable here: insertion(&array_,size);. As you want to pass the pointer just change to: insertion(array_,size); Commented Sep 15, 2015 at 14:35
  • No.It' showing an error.I think that part was right. Commented Sep 15, 2015 at 14:40
  • 1
    Try cout<<(*array_)[i]; Commented Sep 15, 2015 at 14:43
  • 2
    @TanvirRahman Well, the signature of insert() is also wrong, should be void insertion(int *array_,int size){. Otherwise it expects an array of pointers. Commented Sep 15, 2015 at 14:43
  • Yes i found it....:) Commented Sep 15, 2015 at 14:44

1 Answer 1

2
void insertion(int *array_,int size){
    int i;
    for(i=0;i<size;i++){
        cout<<array_[i]<<" ";
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think the way you did it was correct as well, although not used very often. However operator[] has precedence over operator*, so the correct code would be cout << (*array_)[i].
Thanks it is working in your way also.If you want, you can write it in answer section.I will accept is as answer of this question...:)
@TanvirRahman, @rozina I would certainly downvote such answer, because it's totally confusing and not necessary. Stay with what you have now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.