tags:

views:

447

answers:

6

The following code :

int *a;
*a = 5;

will most likely result in a segmentation fault and I know why.

The following code :

int a;
*a = 5;

won't even compile. (gcc says : invalid type argument of unary *).

Now, a pointer is simply an integer, which is used for storing an address. So, why should it be a problem if I say :

*a = 5;

Ideally, this should also result in a segmentation fault.

+1  A: 

You never actually assign "a" in the first case.

int* a = ?
*a = 5; //BAD. What is 'a' exactly?

int a = ? //but some int anyway
*a = 5; //'a' is not a pointer!

If you wish to use the integer as a pointer, you'll have to cast it first. Pointers may be integers, but conceptually they serve different purposes.

luiscubal
+13  A: 

A pointer is not an integer. C has data types to

a) prevent certain programming errors, and

b) improve portability of programs

On some systems, pointers may not be integers, because they really consist of two integers (segment and offset). On other systems, the "int" type cannot be used to represent pointers because an int is 32 bits and a pointer is 64 bits. For these reasons, C disallows using ints directly as pointers. If you want to use an integral type that is large enough to hold a pointer, use intptr_t.

Martin v. Löwis
A: 

int* a — is a pointer to int. It points nowhere, you haven't initialized it. Please, read any book about C before asking such questions.

stepancheg
I explicitly wrote that I know why it fails...My question was why would the second snippet not compile?
dta
A little of bit of class will go a long way to avoiding multiple downvotes on your answers.
unforgiven3
+3  A: 

When you say

int a;
*a = 5;

you are trying to make the compiler dereference something that is not a pointer. Sure, you could cast it to a pointer and then dereference it, like so,

*((int*)a) = 5;

.. and that tells the compiler that you really, really want to do that. BUT -- It's kind of a risky thing to do. Why? Well, in your example, for instance, you never actually initialized the value of a, so when you use it as a pointer, you are going to have whatever value is already at the location being used for a. Since it looks like it is a local variable, that will be an un-init'd location in the function's stack frame, and could be anything. In essence, you would be trying to write the value 5 to some undetermined location; not really a wise thing to do!

JustJeff
+3  A: 

It's said to illustrate that pointers merely store addresses, and that addresses may be thought as numbers, much like integers. But usually addresses have a structure (like, page number, offset within page, etc).

You should not take that by word. An integer literally stores a number, which you can add, subtract etc. But which you cannot use as a pointer. An integer is an integer, and a pointer is a pointer. They serve different purposes.

Sometimes, a cast from a pointer to an integer may be necessary (for whatever purposes - maybe in a OS kernel to do some address arithmetic). Then you may cast the pointer to such an integer type, previously figuring out whether your compiler guarantees correct sizes and preserves values. But if you want to dereference, you have to cast back to a pointer type.

Johannes Schaub - litb
A: 

The operator * is a unary operator which is not defined for the integer data type. That's why the statement

*a = 5;

won't compile.

Also, an integer and a pointer are not the same thing. They are typically the same size in memory (4 bytes for 32 bit systems).

zooropa