Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:04:57+00:00 2026-05-26T16:04:57+00:00

In my code, I am trying to create a dynamic array with initArray function,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:04:57+00:00Added an answer on May 26, 2026 at 4:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create some dynamic code within clojure. In the function below, the
I recently came across this in some code - basically someone trying to create
I'm trying to create Accordian Controlls Dynammically on Page Load, code wise this is
I'm trying to create an associative array with dynamic data, and having some trouble.
How to create dynamic table model in J2ME? like from arrays i use kxml
I am trying to create some dynamic sql using the following code block firstSqlStatement
hi i am trying to use dynamic array name . but when i run
Greetings, coders, Background Info and Code I am trying to create a daemon-type program
I am trying to create a new Element in my javascript code and append
I'm trying to create a piece of code but cannot get it working. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.