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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:56:24+00:00 2026-06-08T03:56:24+00:00

I have a quick question regarding the scope of dynamic arrays, which I assume

  • 0

I have a quick question regarding the scope of dynamic arrays, which I assume is causing a bug in a program I’m writing. This snippet checks a function parameter and branches to either the first or the second, depending on what the user passes.

When I run the program, however, I get a scope related error:

error: ‘Array’ was not declared in this scope

Unless my knowledge of C++ fails me, I know that variables created within a conditional fall out of scope when when the branch is finished. However, I dynamically allocated these arrays, so I cannot understand why I can’t manipulate the arrays later in the program, since the pointer should remain.

        //Prepare to store integers
        if (flag == 1) {
                int *Array;
                Array = new int[input.length()]; 
            } 
        //Prepare to store chars
        else if (flag == 2) {
                char *Array;
                Array = new char[input.length()];
            }

Can anyone shed some light on this?

  • 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-06-08T03:56:25+00:00Added an answer on June 8, 2026 at 3:56 am

    Declare Array before if. And you can’t declare array of different types as one variable, so I think you should use to pointers.

    int *char_array = nullptr;
    int *int_array  = nullptr;
    
    //Prepare to store integers
    if (flag == 1) {
        int_array = new int[input.length()]; 
    } 
    //Prepare to store chars
    else if (flag == 2) {
        char_array = new char[input.length()];
    }
    
    if (char_array)
    {
        //do something with char_array
    }
    else if (int_array)
    {
        //do something with int_array
    }
    

    Also as j_random_hacker points, you might want to change you program design to avoid lot’s of if

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

Sidebar

Related Questions

I have this quick question regarding cost estimation using Function Points. We are doing
this is my very first post! I have a quick question in regarding inheritance
quick question regarding Array's in xcode. I have ht efollowing code, which is supposed
I have a quick question regarding to mobile development, I'm new at this, so
I have a quick question regarding debug and release in VS 2008. I have
Hi I have a quick question regarding json responses from the web services, I
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
Good afternoon, I have a quick 'top level' question regarding the usage of Facebook
I have this quick question, i have got the username variable from a form
I have a quick question regarding the creation of absolute URLs within a symfony

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.