user2005643
user2005643

Reputation:

If pointers are just addresses, why can't I assign a specific int address to a pointer?

As I've been explained, a pointer is a variable that holds an address. Why am I unable to do int *p = 5; then, wouldn't that be correctly pointing to the memory address 5?

Upvotes: 0

Views: 238

Answers (4)

Antoine C.
Antoine C.

Reputation: 3962

You can't assign an address to a pointer like that, because you haven't the right. Maybe the address is already used by some other program or the OS. You have to allocate an address:

int *n;
n = malloc(sizeof(int));

Upvotes: 0

Eric Lippert
Eric Lippert

Reputation: 660279

a pointer is a variable that holds an address

No no no no no.

So many questions in the C tag on StackOverflow could be avoided if people were actually taught the basics. The rules are:

  • A variable is a storage location.

  • A storage location holds a value.

  • A pointer is a particular kind of value.

  • A pointer may be dereferenced. Dereferencing a pointer produces a storage location.

  • A storage location may be addressed. Addressing a storage location produces a pointer.

So let's review your sentence:

a pointer is a variable ...

No, a pointer is a value. There might be a variable that holds a pointer, just like there might be a variable that holds an int. But an int isn't a variable, and neither is a pointer. Similarly, a pointer might be dereferenced to produce the storage location associated with a variable, but that is not saying that the pointer is the variable.

... that holds an address

A pointer by definition is an address, but what an address actually consists of is an implementation detail. A pointer holds something that when dereferenced produces a storage location. A common choice for implementers of C compilers is to make the value of a pointer be a numeric offset into a large block of virtual memory, but there is no requirement that an author of a C compiler use integer offsets into virtual memory as pointers.

There is a requirement that addresses be convertible to integers and that integers be convertible back to addresses, and that these mappings obey certain restrictions on their behaviour. But for all you know the address could be the string "5" and the conversion to integer could be to call atoi on that thing. That would be silly, but it would be legal.

Why am I unable to do int *p = 5;

Who says that you are unable?

The C11 specification says:

An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation. The mapping functions for converting a pointer to an integer or an integer to a pointer are intended to be consistent with the addressing structure of the execution environment. Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

So there you go. If the compiler you are using gives meaning when mapping 5 to a particular address, you go be awesome. If it does not, then you're in for the worst sort of undefined behaviour. Basically there is no requirement on the developer of a C compiler to make any conversions between pointers and integers except that zero is always a null pointer, and no valid object has the address zero.

Upvotes: 4

deviantfan
deviantfan

Reputation: 11434

Yes, theoretically that would be correct
(except address 5 is probably nothing you should use).
Maybe you´ll get a warning/error from the compiler
because assigning a plain number to a pointer is not usual,
but with a cast it will accept it:

int *p = (int*)5;

Upvotes: 1

MByD
MByD

Reputation: 137382

Pointers are data types, not just addresses, and int is a different, not compatible, data type, so you cannot assign an int to a pointer.

If you really need that, and you know what you're doing, you can cast an int (i.e. char * p = (char*)5; ) to the specific data type, but you need such things only in rare cases.

Upvotes: 6

Related Questions