3 ms·
No, it’s correct. The statement makes no claims about how the pointer is stored, but semantically it is a variable with the information in it to locate another
by saagarjha 6y ago
No, it’s correct. The statement makes no claims about how the pointer is stored, but semantically it is a variable with the information in it to locate another variable.
- tom_mellior 6y agoThis is true, as long as your definition of "another variable" stretches far enough to include memory regions allocated by malloc. And (leaving the C standard), also things allocated through other means, like memory-mapped files, and things like like memory-mapped peripherals. In other words, the statement is incorrect.
- grayhatter 6y agoUm, why wouldn't it be correct to call any/all of thoes examples a variable?
- saagarjha 6y agoThey’re not variables until they’ve been specified a type, which is at the point you first assign to the memory in a typed fashion I believe. It’s significantly more pedantic than the first line in an introductory text will go into, though ;)
- grayhatter 6y agoNah, that's wrong. A variable doesn't need a type. Case in point; void.
- saagarjha 6y agoI'm not sure what you mean, could you explain a bit more?
- icedchai 6y agoAll variables have types in C. You can't declare a "void" variable. You can declare a void function. Try assigning it to a variable. You can't. void pointers (void*) are a different story.
- eru 6y agoSo just the opposite of what the comment you replied to suggested: 'void' is a type, but in C values of the type void are not first class. Ie a function can return a value of type void, but you can't assign it to a variable.
- eru 6y agoAre you saying void is not a type?
- saagarjha 6y agoI was specifically responding to the comment above, which had clearly conflated the standard not specifying the representation of a pointer with “anything anyone says about pointers isn’t actually correct”. Keeping the standard in mind the definition of “variable” generally extends to “any typed memory regardless of whether it has a name or not”, and the pointers K&R are talking about are fairly clear to mean this in the sense of the pointer being dereferenceable to a specific type. That being said, you are correct that there are other pointers not mentioned in that statement that are an entirely separate class from normal data pointers: untyped buffers, usually specified by void * but (for legacy reasons) often char * as well. And of course, there’s also function pointers that you dereference only to call them.