Hello, I'm not much of a programmer and will probably never be, but am just curious.
Can I initialize a pointer with a constant value different than NULL? I.e. can I tell my pointer to point at one particular memory cell? It's a number so why couldn't I? I know it would be of little use in normal programming, but I thought it could be could good while programming some devices with very little memory, where one would like to control every cell.

Recommended Answers

All 9 Replies

Yes you can do that.

But details do vary from one compiler to another (and the kind of machine you're running on).

Yes. That's one of the main reasons for having pointers.

Yes you can do that.

But details do vary from one compiler to another (and the kind of machine you're running on).

I'm working on devcpp and it doesn't allow me to initialize with an integer number no matter if I do this in a hex or decimal base. How would I initialize my pointer with a particular address on devcpp? From what compiler says, such syntax:
int* a = 1;
is simply invalid. Then, how do I give a constant address, if not by an integer?

Now you're in the realm of "what the heck are you trying to do" type of question. Just loading a number into a pointer does not make it an address.
You load the pointer with an address, not an integer.
Explain what you are trying to accomplish, with enough detail for us to understand. You'll probably need to post some example code, too.

Now you're in the realm of "what the heck are you trying to do" type of question. Just loading a number into a pointer does not make it an address.
You load the pointer with an address, not an integer.
Explain what you are trying to accomplish, with enough detail for us to understand. You'll probably need to post some example code, too.

I'm not trying to accomplish anything. Except understanding. As far as I know, address is a number. When I write int a = 1; I initialize a with a constant int. Can I do the same with a pointer? I.e. initialize it with a constant address? If so, how do I write this constant address?

A constant int (i.e. 1) is not an address. It's the value 1. You seem to misunderstand what an address is. Maybe a google search can clear up some details.

>If so, how do I write this constant address?
Cast it to the appropriate pointer type:

int *p = (int*)1;

However, it's strongly recommended that you don't do this unless you really know what you're doing.

Thanks, Narue!

> I'm working on devcpp and it doesn't allow me to initialize with an integer number
That's because your target OS is Win32, and absolute addresses are completely meaningless in a virtualised address space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.