2

In my code, I am trying to create a dynamic array with initArray function, and in main I would like to use this initialized array. However, whenever i called the initialized array in main, it is giving me an error.

Here is what i tried:

void main() 
{
    int *a = NULL;
    int n;
    cout<<"Enter size:";
    cin>>n;
    initArray(a,n);
    for(int j=0;j<n;j++)
    {
        cout<<a[j]<<endl;//Crashes here
    }
}
void initArray(int *A, int size)
{
    srand((unsigned)time(0));
    A = new int[size];
    for(int i=0;i<size;i++)
    {
        A[i] = rand()%10;
    }
}

When i do initArray part in main, it works. What am i doing wrong?

7
  • 2
    Use std::vector<T> rather than C style raw arrays is probably a better idea. (As is indenting your code correctly) Commented Nov 6, 2011 at 12:35
  • @Yacoby I want to use the array rather than vectors. I just want to know what i'm doing wrong. Commented Nov 6, 2011 at 12:39
  • @MatteoItalia I know, but this isn't about that right now. Thanks though. Commented Nov 6, 2011 at 12:40
  • @LuckySlevin: that's why I wrote it in a comment an not in an answer. :) Commented Nov 6, 2011 at 12:40
  • Another aside: you should call srand only once, not at every call to initArray, otherwise, if you call it more than once per second, you will always get the same numbers. Commented Nov 6, 2011 at 12:46

5 Answers 5

7

I see two problems:

  1. the function accepts a pointer. When you write A = ... you're only changing the copy of the pointer that gets passed to you by value. You could use void initArray(int* &A, int size) instead, or have the function return a pointer.

  2. if that's the full code, you might need a forward declaration of the initArray function.

Sign up to request clarification or add additional context in comments.

1 Comment

First of all thanks for the answer. That's what i want to hear. I will give it a try. And the second part, it's not full code, i declared initArray function.
5

What am i doing wrong?

Not using std::vector is what you're doing wrong.

That aside, assuming this is for learning or homework or something:

initArray(a,n);

This line copies the int pointer a. The copy inside the function gets assigned, and the one in main will remain empty. You need to use pass-by-reference, either through C++ references or C style with pointers:

void initArray(int*& a, int size){
  // everything the same
}

This will modify the int pointer in main without any other changes.

void initArray(int** a, int size){
  // need to dereference the pointer-to-pointer to access the int pointer from main
  *a = new int[size];

  for(/*...*/){
   (*a)[i] = /*...*/;
  }
}

For this one, you'll need to change the call side too:

initArray(&a, n); // pass pointer to a

Now one last thing: main doesn't even know that initArray even exists. You need to put it above main or atleast forward declare it:

void initArray(int*& a, int size); // forward declaration

int main(){
  // ...
}

void initArray(int*& a, int size){
  // ...
}

And one last thing, you'll need to delete[] the array in main.

1 Comment

Thanks I already accepted another answer. I will give vote up for your help. Thanks a lot.
3

You need to put the initArray function definition above main or at least declare it above main.

Also note that the assignment A = new int[size]; only modifies the local variable A inside the function initArray. It has no effect on the a inside main because pointers are passed by value. You need to either pass a reference or a pointer to the pointer, or better yet, return the pointer:

int* initArray(int size)   // note: one less parameter
{
    srand((unsigned)time(0));
    int* A = new int[size];
    for(int i=0;i<size;i++)
    {
        A[i] = rand()%10;
    }
    return a;   // note: return the pointer
}

And then write a = initArray(n); inside main. Also, don't forget delete[] a; inside main!

And of course the usual advice, don't use raw pointers to dynamic arrays, use std::vector instead. But since I guess this is homework for a crappy C++ course, you probably have no choice.

1 Comment

Thanks I already accepted another answer. I will give vote up for your help. Thanks a lot.
1

You are passing a copy of your pointer to initArray, make it a reference initArray(int *&A, int size)

#include <iostream>
using namespace std;

void initArray(int *&A, int size); // put the declaration of function here


int main() 
{
    int *a = 0;

    int n;
    cout << "Enter size:";
    cin >> n;

    initArray(a,n);

    for(int j=0;j<n;j++)
    {
        cout<<a[j]<<endl;
    }

    delete[] a; // delete your array before exiting the program
}

// pass it as a reference to pointer
void initArray(int *&A, int size)
{
    srand((unsigned)time(0));
    A = new int[size];

    for(int i=0;i<size;i++)
    {
        A[i] = rand()%10;
    }
}

1 Comment

Thanks I already accepted another answer. I will give vote up for your help. Thanks a lot.
0

you cna change the dclaration of initArray :

void initArray(int* &A, int size)

in your initial code, the array is not filled in. because, A array is a copy of the main a array. so the a array remains equals to null. you have then the choice between of passing a by reference "int* &A" as i did or by adress : "int** A"

Comments

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.