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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You’re right on the money with
char
s,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 whysizeof
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 allocateX * sizeof(type)
for that pointer, and you would want to advance it bysizeof(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.