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 7488187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:46:50+00:00 2026-05-29T14:46:50+00:00

I am trying to make a new array in my project the code is:

  • 0

I am trying to make a new array in my project

the code is:

#include <iostream>
using namespace std;
void makeArray( int *& arrayPtr, int size );
int main( )
{
  int * arrPtr;
  int size =10;
  makeArray( arrPtr, size );
  for(int j=0;j<size;j++)
  {
    cout<<arrPtr[j]<<endl;
  }
}
void makeArray( int *& arrayPtr, int size )
{
  int arr[size-1];
  for(int i=0;i<size;i++)
  {
    arr[i]=0;
  }

  *&arrayPtr=*&arr;
}

According to the requirements i need to use the above “makeArray” method inorder to make the array.
When i run the code the output is garbage values not zero…..

any help will be appreciated

thank you

  • 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-29T14:46:53+00:00Added an answer on May 29, 2026 at 2:46 pm

    The way you are creating the array is on the stack, which means that it will not exist after the makeArray function finishes.

    You will need to allocate the array on the heap.

    So:

    int arr[size-1];
    

    should be:

    int *arr = new int[size-1];
    

    Also, I think you mean to do this in makeArray():

    arrayPtr = arr;
    

    Instead of:

    *&arrayPtr=*&arr;
    

    Which compiles but is more complex and is functionally the same thing in this context.

    But you may prefer just returning an int* instead of taking a reference to the pointer.

    Then when you are done using the array in main(), and set it to NULL just in case you accidentally use it again, like this:

    for(int j=0;j<size;j++)
    {
      cout<<arrPtr[j]<<endl;
    }
    delete [] arrPtr;
    arrPtr = NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a method that expects an array of int and two
I'm trying to make a new spine javascript app using d3 (generated using Spine.app).
Hey, Im trying to make a nested array in JS var lines = new
I'm trying to make a new CLLocation subclass. This is the skeleton: #import <UIKit/UIKit.h>
I'm trying to make a new wx.Choice-like control (actually a replacement for wx.Choice) which
I don't know alot about ASP.Net but I'm trying to make a new control
I'm new to BIRT and I'm trying to make the Report Engine running. I'm
I am trying to make a copy of a database to a new database
I am trying to make a firefox extension which looks at all the new
I am trying to make a blackjack game where before each new round, 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.