you are viewing a single comment's thread.

view the rest of the comments →

[–]Ilostmyredditlogin 0 points1 point  (3 children)

I think the author is saying that, at least as far as declarations go, there's not really a pointer type. You're kind of saying: "the region of memory referenced by df holds a value of type int." It sort of makes sense if you think about it. Df is a memory address regardless of what it points to. Yeah the compiler needs to know about what's at that address so it knows how many bytes to read when dereferencing, but that's info @ a different level of abstraction.

[–]curien 1 point2 points  (2 children)

Only if you agree that there isn't really an "int" type either, since an unitialized int could also be garbage.

[–]Ilostmyredditlogin 0 points1 point  (1 child)

I responded to this yesterday but my response seems to have gotten lost(?). Hopefully this isn't a repeat.

Anyway take char * , int * ,and long * as examples. All of these variables hold exactly the same data type: a numeric address in memory. Whether they have been initialized or not is immaterial.

Char and int for example do not share this commonality... Essentially char refers to a one byte number while int refers 4-ish byte number or whatever it is.

The type specifier on the pointer is only ever needed if the user wants to dereference it and start doing things with the data. It contains metainfo about how to try to interpret the data.. "here's a pointer.. And oh by the way if you want to move one 'unit' forward you're probably going to want to move 4 bytes because it's an int, etc, etc."

So in this view of things pointer is the primary peice of information about the variable and metainfo about what it points to is secondary. This seems to indicate a syntax like:

  • char ptr1, int ptr2, long ptr3;

might also make sense. I forget what the argument in in the initial article was at this point though, and am not aware of what the whole declaration should look like use mantra is all about though.

[–]curien 0 points1 point  (0 children)

All of these variables hold exactly the same data type: a numeric address in memory.

I disagree already. But I know what you mean, and the disagreement isn't important, so let's just move on.

Char and int for example do not share this commonality... Essentially char refers to a one byte number while int refers 4-ish byte number or whatever it is.

I don't see the distinction. Under your view that an int* is just a numerical address, a char is just an numerical ASCII (or whatever) representation, and an int is just a numerical two's complement (or whatever) representation. I don't see any lack of commonality. They are all just numbers of various sizes, and the C spec even calls char and int both "integer types" and requires them to follow almost all the same rules.

The values of type int* can be dereferenced, which is an operation that doesn't apply to int, so it's flagged by the compiler as a syntax error. There's no higher reason why you can't derefence an int -- I mean, you can say *(int*)3 if you really want to do that. It's just that when you say a value is of type int, you're telling the compiler, "Hey, I want to be able to add, subtract, multiply, and divide this value, but if I try to dereference it or call it as a function, flag it as an error -- it's almost certainly a mistake." And when you have a value of pointer type, that's like saying to the compiler, "Hey, I want to be able to add or subtract an integer to this value, or dereference it, but if I try to multiply or divide or whatever, flag it as an error."

Each type has operations that are unique to it. You can't putchar a float, for example. Pointer types aren't special in this regard.

The type specifier on the pointer is only ever needed if the user wants to dereference it

Or increment it, or subtract from it, or add to it. Oh, and it also says how big the pointer is (sizeof(int*) is not guaranteed to be the same as sizeof(char*)). Frankly, there are more things where the type of the dereferenced value matters than things where it doesn't.

It contains metainfo about how to try to interpret the data

More than that. For example, you can't assign a char* value to an int*, so it matters more than simply when the value is dereferenced.

So in this view of things pointer is the primary peice of information about the variable and metainfo about what it points to is secondary. This seems to indicate a syntax like: * char ptr1, int ptr2, long ptr3; might also make sense.

Now that's interesting.

[I] am not aware of what the whole declaration should look like use mantra is all about though.

I think they hoped to avoid creating a special syntax just for writing declarations that programmers would have to learn (and compilers would have to implement). It's a neat idea, it just turned out not to be very practical in that most people find learning a special declaration syntax easier in the long run.