1

Whats the difference between integer and the address returned by & ?

Therefore, why should I type-cast integer to an integer pointer when assigning it to an integer pointer?

Alan Salios
  • 241
  • 2
  • 9
  • 1
    A pointer points to a address, but in your first example you are assigning a address to a int and not a pointer – Rizier123 Feb 05 '15 at 07:22
  • 4
    In short, a pointer isn't just an address, it also has a type. – Yu Hao Feb 05 '15 at 07:23
  • 1
    Integers by default are signed, meaning, it could be negative or positive. In the case of an address, it must be unsigned. Address of something cannot be negative. –  Feb 05 '15 at 07:24
  • @YuHao OTOH the language allows for all kinds of implicit type conversions. – juanchopanza Feb 05 '15 at 07:25
  • something like `unsigned const int` ? – Alan Salios Feb 05 '15 at 07:25
  • 4
    This is like asking what is the difference between an apple and an orange... yeah you could paint the apple orange and see if anyone notices the difference – M.M Feb 05 '15 at 07:26
  • @MattMcNabb Okay this is the best example to explain it :D(by far) – Rizier123 Feb 05 '15 at 07:27
  • Great. Now I am even more confused. Thought it's impossible. – Alan Salios Feb 05 '15 at 07:28
  • @Alan Salios: Yeah exactly, It is just another integer but unsigned. You could do operations on it like addition etc. –  Feb 05 '15 at 07:32
  • @Rizier123 and maybe the best _non-technical_ explanation to a _technical_ problem. – Sourav Ghosh Feb 05 '15 at 07:32
  • 4
    Really, it is best to **not** think of pointer as "just another integer". Treat them as apple and orange. Then there is no chance of confusion. On systems with segmented memory model they aren't just another integer anyway. – M.M Feb 05 '15 at 07:34
  • Then why it produces these warnings. It makes me think I am doing something quite funny. – Alan Salios Feb 05 '15 at 07:34
  • @AlanSalios yes, your code is illegal (i.e. violates the C standard). – M.M Feb 05 '15 at 07:34
  • "Illegal" ? Okay might that cause any other problem but a warning message? – Alan Salios Feb 05 '15 at 07:35
  • @elf I'm not so sure about "address cannot be negative". That heavily depends on the architecture - maybe there is one with 2 banks of memory. But in general, attributes like "negative" just don't apply to addresses... – glglgl Feb 05 '15 at 07:35
  • @AlanSalios yes, it could cause missiles to be launched or demons to fly out of your nose. [See here](http://stackoverflow.com/a/4105123/1505939) for further explanation. You seem to be ignoring the fact that C has a type system. – M.M Feb 05 '15 at 07:38
  • 2
    @elf: Addresses in C are neither signed not unsigned. They're not numbers. – Keith Thompson Feb 05 '15 at 07:38
  • @Keith: Then can you explain operations like addition, increment etc that c allows us to perform? –  Feb 05 '15 at 07:40
  • @elf, this is *pointer* arithmetic, and not integer arithmetic, and heavily uses the type the pointer is pointing to. And it is only guaranteed to work as long as you stay *inside* an array object. With your argument `double` would be an integer type, too. – Jens Gustedt Feb 05 '15 at 07:43
  • Pointer arithmetic. Wasn't it the number you add multiplied by the bytes of the data type. – Alan Salios Feb 05 '15 at 07:44
  • Pointers points to memory locations. It is considered a number whether it contains segment descriptors or offsets. Os memory manager handles it internally and not the c programmer. Address cannot be negative, only the offsets in certain architectures. But it doesn't matter in a c program. Does it? –  Feb 05 '15 at 07:44
  • @Alan Salios: Yeah ofcourse, but those things are transparent to a programmer right? You dont have to do shift and add yourself. do you? –  Feb 05 '15 at 07:46
  • I think everything is a number after all, represented in different ways by the compiler. So I disagree with Keith. – Alan Salios Feb 05 '15 at 07:48
  • @AlanSalios, your question has changed fundamentally. This is not a forum for discussion. And no, your assumption is wrong, you don't have to cast a pointer types for assignment. In modern C casts are almost always an error. – Jens Gustedt Feb 05 '15 at 07:49
  • My question didn't change. I just removed the unnecessary parts which clarified the question, since I think it wasn't accurate enough. – Alan Salios Feb 05 '15 at 07:50
  • I agree with you guys that it is not just another number. All I said was it is unsigned and that is one of the differences. Certain operations are not allowed on pointers. His question was "what is the difference between an integer and an address?" and I was pointing out of of the differences. –  Feb 05 '15 at 07:55
  • @elf: No, a pointer *in C* is not a number *at all*. Try multiplying two pointers. – Keith Thompson Feb 05 '15 at 08:05
  • @Keith read my previous comment. –  Feb 05 '15 at 08:10
  • Again I don't agree with you that it is not a number. Certain operations are restricted because of the logic of pointers. Addresses cannot be multiplied or divided because it might result in illegal memory locations, segment faults, overflows and anything and everything. It doesn't mean a pointer is not a number. It is just like a closure on a subset of integers with specific operations defined. –  Feb 05 '15 at 08:14
  • @elf: I already did. The latest draft of the C standard is [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). Can you cite it to support you position? – Keith Thompson Feb 05 '15 at 08:15
  • Tell me how do you access a memory location in von-newmann architecture? –  Feb 05 '15 at 08:18
  • @elf C is not restricted to von Neumann architecture. Also, adding and subtracting addresses might result in illegal memory locations so that argument doesn't work. – M.M Feb 05 '15 at 08:26
  • See my previous comments. I completely agree with you. All I said was It is a number. Probably a closure on a subset of unsigned integers. –  Feb 05 '15 at 08:31

2 Answers2

3

Integer and pointer to integer are different types.

  • Integer variable holds integer value.
  • Pointer variable holds address value.

Given your example:

int value = 5;
int address = &value;
  1. value is variable of type int.
  2. &value returns address of type int*
  3. address = &value tries to assign int* to int.

While integers can be converted to pointers and back, it doesn't mean that addresses are necessarily integer values.

Just like you can assign double to int, you can assign int * to int. But it's not necessarily what you want, and that's why compiler warns about it. In fact, unless you are working very close to hardware (writing driver for example), it's very unlikely you need to do conversions between integers and pointers.

Correct way to do this, without needing any casts:

int * pointer = &value;
user694733
  • 15,208
  • 2
  • 42
  • 68
  • Correct, except for one small quibble. Yes, you can assign a `double` to an `int`; the value is implicitly converted. You cannot assign an `int*` to an `int`. What you can do is explicitly convert an `int*` to type `int`, and then assign the resulting `int` value to an `int` object. – Keith Thompson Feb 05 '15 at 15:51
  • @KeithThompson You are right, in standard `double`/`int` are arithmetric types and pointers are not. However, in OPs unedited post he did get only warning, and in practice both situations tend to behave the same way: compiler will do the implicit conversion and give only warning for it. – user694733 Feb 06 '15 at 08:10
  • @KeithThompson I have gotten too used to non-GCC compilers. It seems GCC doesn't warn about `double` to `int` assignment even with `-Wextra`. – user694733 Feb 06 '15 at 08:22
0

Whats the difference between integer and the address returned by & ?

I think this question has already been answered in the comments. The difference is the type of those variables. Basically int = Integer type and *int = Pointer type.

Furthermore: Conversion from pointer to int can result in undefined behavior.

Therefore, why should I type-cast integer to an integer pointer when assigning it to an integer pointer?

Because the standard says:

C99 Section 6.5.4 and 6.5.16.1:

Conversions that involve pointers, other than where permitted by the constraints of 6.5.16.1, shall be specified by means of an explicit cast.

And C++ Section 5.4:

Any type conversion not mentioned below and not explicitly defined by the user (12.3) is ill-formed.

Sambuca
  • 1,224
  • 18
  • 30
  • Also important is that conversion from pointer to `int` might cause undefined behaviour – M.M Feb 05 '15 at 08:27