4 ms·
If the name of a function is already the memory address of the function, what extra information does the pointer store? Couldn't you just assign the name of the
by felxh 13y ago
If the name of a function is already the memory address of the function, what extra information does the pointer store? Couldn't you just assign the name of the function to, say, an int?
- vidarh 13y agoYou can coerce it to an int with a cast, with some caveats: It is implementation defined whether or not the size of an int is sufficient to store a pointer, and the type lets the compiler know what you want to happen when you operate one it. If "foo" is an integer variable, then "foo + 1" returns the integer value of foo + 1, but if "foo" is a pointer, "foo + 1" returns the value of foo + the size of a pointer, for example. So the pointer does not store any extra information, but the compiler stores and makes use of extra information about the pointer variable (not about the value itself) during compilation.
- felxh 13y agoThat makes a lot of sense, thanks!
- emillon 13y agoYou can, but it makes more sense to assign it to a void \*. After all, it's a pointer but you don't know the size of the pointed object. Note: this is not true everywhere since (data) pointers and function pointers can be represented differently, but on a PC it works :)