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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:21:14+00:00 2026-05-29T22:21:14+00:00

I am learning about pointers and the new operator in class. In my readArray

  • 0

I am learning about pointers and the new operator in class.

In my readArray function I am to read in a size. Use the size to dynamically create an integer array. Then assign the array to a pointer, fill it, and return the size and array.

I believe I’ve gotten that part corrected and fixed but when I try to sort the array, i get the error “uninitialized local variable temp used.”

The problem is though I get that error when I am trying to intialize it.
Any help appreciated thank you. Seeing my errors is very helpful for me.

#include <iostream>
using namespace std;

int* readArray(int&);
void sortArray(int *, const int * );

int main ()
{
    int size = 0;
    int *arrPTR = readArray(size);
    const int *sizePTR = &size;
    sortArray(arrPTR, sizePTR);

    cout<<arrPTR[1]<<arrPTR[2]<<arrPTR[3]<<arrPTR[4];

        system("pause");
        return 0;
}


int* readArray(int &size)
{
    cout<<"Enter a number for size of array.\n";
    cin>>size;
    int *arrPTR = new int[size];

    for(int count = 0; count < (size-1); count++)
    {   
         cout<<"Enter positive numbers to completely fill the array.\n";
         cin>>*(arrPTR+count);
    }

    return arrPTR;
}

void sortArray(int *arrPTR, const int *sizePTR)
{
    int *temp;
    bool *swap;

    do
    {
        swap = false;
        for(int count = 0; count < (*sizePTR - 1); count++)
        {
            if(arrPTR[count] > arrPTR[count+1])
            {
                *temp = arrPTR[count];
                arrPTR[count] = arrPTR[count+1];
                arrPTR[count+1] = *temp;
                *swap = true;
            }
        }
    }while (swap);
 }
  • 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-29T22:21:15+00:00Added an answer on May 29, 2026 at 10:21 pm

    You declared temp as a pointer. You need to allocate it on the heap before dereferencing and assigning to it later. However perhaps a variable on the stack would be preferable?

    FYI: You should be aware of the memory leak in readArray as well which is leaving callers responsible for calling delete []

    Edit: I hope this will help clear up some of the other problems.

    #include <iostream>
    
    int* readArray(int&);
    void sortArray(int*, int);
    
    int main ()
    {
        int size(0); // use stack when possible
        int *arrPTR = readArray(size);
        sortArray(arrPTR, size);
    
        // arrays are zero based index so loop from 0 to size
        for (int index(0); index < size; ++index)
            std::cout << arrPTR[index];
    
        delete [] arrPTR; // remember to delete array or we have a memory leak!
        // note: because we did new[] for an array we match it with delete[]
        //       if we just did new we would match it with delete
    
        system("pause");
        return 0;
    }
    
    int* readArray(int& size)
    {
        std::cout << "Enter a number for size of array.\n";
        std::cin >> size;
        int *arrPTR = new int[size]; // all news must be deleted!
    
        // prefer pre-increment to post-increment where you can
        for(int count(0); count < size; ++count)
        {   
             std::cout << "Enter positive numbers to completely fill the array.\n";
             std::cin >> arrPTR[count];
        }
    
        return arrPTR;
    }
    
    // passing size by value is fine (it may be smaller than pointer on some architectures)
    void sortArray(int *arrPTR, int size)
    {
        // you may want to check if size >= 2 for sanity
    
        // we do the two loops to avoid going out of bounds of array on last iteration
        for(int i(0); i < size-1; ++i) // the first to compare (all except last)
        {
            for(int j(i+1); j < size; ++j) // the second to compare (all except first)
            {
                // do comparison
                if (arrPTR[i] > arrPTR[j]) // from smallest to biggest (use < to go from biggest to smallest)
                {
                    // swap if needed
                    int temp(arrPTR[i]); // put this on stack
                    arrPTR[i] = arrPTR[j];
                    arrPTR[j] = temp;
                }
            }
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to C and I have some problems learning about pointers.
I am just now learning about function pointers and, as I was reading the
I'm learning the use of boost smart pointers but I'm a bit confused about
I'm currently learning about pointers in my C++ Algorithms class and while I understand
I am learning about smart pointers ( std::auto_ptr ) and just read here and
I learning to use Pointers. I have a few questions about an exercise code
I am writing a program for a class assignment on learning about pointers and
I am learning about pointers and arrays in class. My trouble is in the
I'm learning about table design in SQL and I'm wonder how to create a
I am learning C programming and I have a simple question about pointers... I

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.