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

  • Home
  • SEARCH
  • 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 8933179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:28:42+00:00 2026-06-15T09:28:42+00:00

I want to declare and initialize a pointer, with a value (like 1000 )

  • 0

I want to declare and initialize a pointer, with a value (like 1000) and I don’t wanna use second variable. Please see below :

int *p = &1000;
Output: error : & on Constant

int *p = (int *)1000;
Output: 0000003E8

int *p = new int(1000);
Output: 1000

Since, first two methods are not giving the expected output. So, I would like to know, which one would be the correct method and why ?

Reference

  • 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-15T09:28:43+00:00Added an answer on June 15, 2026 at 9:28 am

    According to your comment,

    when i try to *p then, it display 1000. Now, whatever you got.

    you would like to initialize the pointer such that it points to a place that stores the integer 1000. You do not want to initialize the pointer such that it points to the absolute address 1000.

    Of the three lines of code you offered, only the last one does what you want:

    int *p = new int(1000);
    

    This allocates space for an int on the heap and value-initializes that space with 1000.

    Notes:

    • Allocating space for a single int on the heap may sometimes be necessary, but most of the time it won’t be useful, because an int is a very small object. The pointer may very well be just as large or larger, therefore passing around a pointer to an int, rather than passing around the int itself, is of limited use.

    • If you really think you need this, keep in mind that you’ll need to deallocate that space later, using

      delete p;
      
    • On a general note, most of the time you need to allocate space on the heap and maintain pointers to it, you are far better off using smart pointers (such as std::unique_ptr<int> or std::shared_ptr<int> in C++11) to avoid having to think of deallocation.

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

Sidebar

Related Questions

I declare the private variable as private List<MultipartFile> files; When I want initialize it
I want to declare an object in java that is like a a pointer
I'm working with some memory pointers. I don't want to use hash defines, please
I want to use CodeDOM to both declare and initialize my static field in
I want to declare static(or not static) variable inside Enum. I need this because
I declare an array of structs and want to declare a second into which
I am using sqlserver 2008 ,I want to initialize and increment variable ( @NUMTwo
I don't want to use pointers when I don't have to, but here's the
I want to initialize a static collection within my C# class - something like
I can: declare @idOrder int set @idOrder = 21319 I want: declare @idOrder int

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.