simplfuzz
simplfuzz

Reputation: 12895

Another C pointer Question

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.

Upvotes: 1

Views: 823

Answers (6)

stepancheg
stepancheg

Reputation: 4276

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.

Upvotes: -4

Paul Kelly
Paul Kelly

Reputation: 4019

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).

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506857

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.

Upvotes: 2

JustJeff
JustJeff

Reputation: 12980

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!

Upvotes: 3

Martin v. Löwis
Martin v. Löwis

Reputation: 127447

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.

Upvotes: 13

luiscubal
luiscubal

Reputation: 25121

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.

Upvotes: 1

Related Questions