Reputation: 31397
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 ?
Upvotes: 0
Views: 2944
Reputation: 313
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*/
Upvotes: 0
Reputation: 1601
#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.
Upvotes: 0
Reputation: 69967
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.
Upvotes: 1
Reputation: 3336
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.
Upvotes: 0
Reputation: 490108
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.
Upvotes: 2
Reputation: 7193
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;
Upvotes: 0
Reputation: 24895
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.
Upvotes: 0