4 ms·
This is a pretty basic article, but might be useful for some. One point I take issue with: a pointer in C is not simply a variable containing an address. It als
by dicroce 13y ago
This is a pretty basic article, but might be useful for some.
One point I take issue with: a pointer in C is not simply a variable containing an address. It also knows (at compile time) how big the object pointed to is and so increments and decrements mean different things depending on the pointer type. In addition, void* is special because it does NOT know it's size.
- intull 13y agoHi there, this was actually targeted at beginners and average C programmers. I'll add your comments in an edit soon! Thanks!
- deleted 13y ago[deleted]
- oscargrouch 13y agoa pointer to anything, no matter what type it is, is ALWAYS the same size as a variable or embeded in a struct, (8 bytes in 64 arch and 4 bytes in 32 bit) cause it only needs to keep the address in memory to the thing its pointing to.. thats where the 4GB of memory limit for 32 bit architecture came from..cause you could not point to any memory address that would not fit into a 4 byte int i think what you are saying is the thing the pointer is pointing to, dont have a known size if its a pointer of void.. since its the only untyped type of pointer.. and the programmer would need to check the size of the thing being pointed in memory at runtime.. (so sizeof wouldnt do it) Also, if you incremment/decrement a pointer , no matter what type it is, you will have a "cursor to the memory" and can even overflow and see memory beyond what you have allocated.. thats where the nasty bugs come from :) the type in the pointer will only tell (when you increment) by how much this incrementation will go for each iteration.. So if its a integer pointer you will advance 4 by 4 (in 32 bit) and if its a void* 8 by 8.. .. a small int 2 by 2.. a char .. 1 by 1.. etc..
- Peaker 13y agoFirstly, a data pointer is allowed to be a different size from a function pointer (although in most platforms they are the same size). Secondly, what he said is that pointers are not just addresses, but typed addresses that know what type they're pointing at.
- WorksOnRobots 13y agoExactly. char * a = 0; int * b = 0; a + 1 = a + sizeof(char) = 1 b + 1 = a + sizeof(int) = 4
- oscargrouch 13y ago> Secondly, what he said is that pointers are not just addresses, but typed addresses that know what type they're pointing at. Hum Ok! At first i thought he was disagreeing about pointer representing addresses; now with the way you have put it, and by reading again, i see he is adding more information to it.. thats not only address but a typed address (unless void of course).. Sorry for misunderstanding.. i want to reiterate that i agree with you "dicroce" ;)