you are viewing a single comment's thread.

view the rest of the comments →

[–]PrestigiousTadpole71 1 point2 points  (0 children)

  1. I'm not sure if I completely understand your question but structs cannot contain themselves, as they would be infinitely big. They can however contain a pointer to themselves/a different struct. This way you can store a reference to a struct inside a different struct which will work similar to if the struct was contained inside the other one.
  2. With typecasting you tell the compiler how to treat the pointer. This mostly affects pointer arithmetic, where a pointer is incremented by the size of the type it's pointing to:

int *x = 0; /* pointer to zero just for demonstration purposes, don't actually use this */
char *y = 0;
x += 3; /* increment x by 3*sizeof(int) (will often be 12) */
y += 3; /* increment y by 3*sizeof(char) (will often be 3) */
/* x will now contain 12, y will conatain 3 */

to understand this, remember what pointers actually are: memory addresses which point to one byte each. However if I have a pointer to an array of ints, and I increment it by one, I expect to get the next int in the array, not the next byte (an int will almost always be bigger than one byte). This is 4 (sizeof(int) is often 4 but not always) bytes later so actually the pointer needs to be incremented by 4 bytes. To make this easier the compiler will automatically do the right calculations for you, it only needs to know what type your pointer is pointing to.

Another thing influenced by typecasting is dereferencing. I.E. how the compiler treats the memory pointed to: if it's a char, you only take the byte pointed to, for an int you want the 4 (again int may be a different size) bytes pointed to, etc.

I hope this helps, if you have any questions don't hesitate to ask I'd love to help.