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

10
  • 1000 isn't a variable. The usually correct way would be int var = 1000; int *p = &var;. If you need to erase the tie to the variable, use a smart pointer. Commented Nov 30, 2012 at 4:36
  • What exactly do you want your pointer to point to? Also, how are you getting the output shown (since 3E8 in hex is equal to 1000 in decimal)? Commented Nov 30, 2012 at 4:36
  • 3
    The accepted answer you refer to says that the first one cannot be used. Commented Nov 30, 2012 at 4:37
  • 1
    By the way, 0x3E8 is equivalent to 1000. Commented Nov 30, 2012 at 4:40
  • 2
    What do you mean by "initialize with a value"? Do you want the value of the pointer itself (i.e. the address it points to) be 1000, or do you want the value stored at that address to be 1000? Commented Nov 30, 2012 at 4:51

7 Answers 7

2

The whole point of a pointer (no pun intended) is to point at another variable (or at least at some memory -- can be a chunk of dynamically allocated memory, as in your third example, instead of an actual variable.

Trying to initialize it with an integer literal simply doesn't make much sense. Initializing it with an integer literal cast to a pointer type makes sense only under extremely limited circumstances -- it gives you the ability to read/write that absolute memory address directly. This can make sense on something like a small embedded system that may have something like a memory mapped device you can access at that address. Otherwise, it's pretty much pointless and useless.

As an aside on terminology: you're actually defining a pointer, not just declaring it. A declaration would be something like:

extern int *p;

This tells the compiler about the existence of a pointer that's defined somewhere else (i.e., in some other translation unit). It's most often seen in a header. Since it's only telling the compiler about the pointer, it can't include a declaration.

Sign up to request clarification or add additional context in comments.

1 Comment

And the embedded system comment is probably the only one surrounding this topic that features a valid use of hacky pointers.
1

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.

Comments

0

The address of operator cannot be applied to constants as mentioned in your reference page itself.

It is unclear as to why you would want to initialize a pointer in this way. It is best that you assign the constant to another variable and use the address of operator on them.

If the pointer value will be changed somewhere down in the code, then it is best to initialize the pointer with NULL.

Comments

0

The first one is wrong (as pointed out in the accepted answer and by your compiler)
The second one seems to be correct but I am not sure what you are displaying. You might be displaying the address represented by p instead of it's value. Try displaying *p instead of just p
The third one seems to be correct but only for C++ and not C
EDIT:
And this is how you would implement Jay's answer

int a = 1000;
int *p;
p=&a;

4 Comments

for second case, i tried p then it shows mentioned output. but, when i tried *p. then system crashed.
Displaying *p for the second one would be a bad idea.
@ Chris: shouldn't p be the memory address and *p the value?
@user13267, Who says you own memory address 1000? Chances are you don't.
0

int * p = new int; *p = 1000;

Thats's what you want. First string allocates memory for an integer, second initializes it with 1000. Don't forgive to write "delete p;" to free memory associated with p when you don't have any need of it further in program.

4 Comments

2nd part of my question is, which one would be the best solution, since 2nd and 3rd are showing some output.
@coders: The second is near useless unless you're writing an OS or directly using memory-mapped I/O. (If you have to ask what that means, you're not doing it.) The third works, but you better remember to delete p; when you're done with the memory.
@chris: Quite preferable, if there's C++11 support. The OP was specifically asking about initializing a regular old int pointer, though, so i dunno how applicable using a unique_ptr would be.
@cHao, Sure if it's the value they really want to be 1000, as half the answers are thinking. Anyway, boost::shared_ptr<int> p = boost::make_shared<int>(1000); :p
0
#include<stdio.h>

int main()
{
    int *ptr = (int *)1000;

    printf("%d\n", ptr);
    return 0;
}

But this is not the correct way to assign value to the pointer.

2 Comments

that's my question, although it won't show any error and give you correct output.
Pointers are just like normal variables, we can assign it any value, but assigning manual address is illegal and it won't be accessible.
0
int *p = &1000;         /*You cannot use & of 1000 As 1000 is the numeric constant not the address of any variable*/

int *p = (int *)1000;   /*You are trying to do casting the value into the pointer so the pointer p will hold the address 0x000003e8 and the address will be pointing to some garbage value*/

int *p = new int(1000); /*In this statement you are trying to allocate the address of some variable form heap and storing a "1000" in that address*/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.