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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:16:06+00:00 2026-06-18T04:16:06+00:00

Lets say in my main method, I declare a const int array pointer pointing

  • 0

Lets say in my main method, I declare a const int array pointer pointing to an array created on the heap. I then want to initialize it’s values (using the memory address) in a constructor TryInitialize() and then print them out. This is not working and I’m wondering what I’m doing wrong? Thanks!

#include "stdafx.h"
#include "part_one.h"
#include <string>
#include <iostream>

using namespace std;

string createTable(unsigned int* acc, double* bal, int n) {
    string s;
    char buf[50];

    for (int i = 0; i < n; i++) {
            sprintf_s(buf,"%7u\t%10.2f\n",acc[i], bal[i]);
            s += string(buf);
    }

    return s;
}



int _tmain(int argc, _TCHAR* argv[])
{

    const int *tempInt = new const int[4];
    TryInitialize(tempInt);
    std::cout << tempInt[1] << endl;

    system("pause");

    return 0;
}

And here is my code for my constructor:

#include "part_one.h"


TryInitialize::TryInitialize(void) {

}

TryInitialize::TryInitialize(int constInt[]) {
    constInt[0] = 8;
    constInt[1] = 0;
    constInt[2] = 0;
    constInt[3] = 8;
}
  • 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-18T04:16:07+00:00Added an answer on June 18, 2026 at 4:16 am

    You should not change a const value.

    For what you trying to accomplish I’d recommend declaring a non-const pointer and a const pointer and assigning the non-const one to the const one after the initialization:

    int _tmain(int argc, _TCHAR* argv[])
    {
        const int *tempTempInt = new int[4];
        TryInitialize(tempInt);
        const int* const tempInt = tempTempInt;
        std::cout << tempInt[1] << endl; //this is now constant.
    
        system("pause");
    
        return 0;
    }
    

    Also pay attention where you put the const in the pointer declaration:

    const int* const tempInt = tempTempInt;
    

    In the declaration above the second const means that you cannot change the pointer; the first const means that you cannot change the pointed value(s).

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

Sidebar

Related Questions

So, lets say in my main activity, i have an array declared like this,
So, the question might seem strange, but let's say that I compile: int main(void)
lets say i created a virtualenv called venv (virtualenv venv) From reading tutorials, i
Lets say we have a main class with 5 buttons, where when each clicked
Lets say I have a class as below: public class class1{ private int[] array1;
Lets say I have an interface like this: public interface MyInterface { int Property1
Lets say I have a Spring MVC form like this: <form:form action=${pageContext.servletContext.contextPath}/secure/main.htm commandName=secure/main> <form:input
Lets say that we have a box class: class Box { private int width;
I came across this scenario. We have a class lets say Main having a
Lets say I have this program below. Now I want to pass both 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.