DEV Community

Discussion on: Why use pointers at all.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Let's check my loose understanding of char. char represents an integer type uint8 from 0 - 255 which is somehow synonymous with utf8 encoding. char* is somehow array like of chars by reference?

I think malloc does direct allocation of a value to memory? Not sure about the rest.

Thank you, This is fun!

Thread Thread
 
jvarness profile image
Jake Varness

You’re right on the money with chars, malloc doesn’t necessarily allocate a value, it allocates a segment of memory for that pointer based on how many bytes you tell it to allocate. This is why sizeof is an extremely important function to use when working with pointers, because it tells you how large any variable or struct is in bytes. If you wanted to make a pointer that was large enough to hold X amount of any type, you’d want to allocate X * sizeof(type) for that pointer, and you would want to advance it by sizeof(type) to get to the next thing it points to.

Pointers are what make me not like programming in C because it’s where a lot of very common problems can happen: segmentation faults happen a lot and the same code that might run fine on one OS might not behave the same on another.