Pointers also allow you to keep references to multiple variables of the same type, kind of like an array, except with the help if malloc, calloc, and realloc you can point to as many variables of the same type as you want, provided the memory still has space to allocate.
Pointers enable you to have things like Strings in your C code (in the form of char*) since Strings arenβt native to C.
Howβs it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK π¬π§
Education
10 plus years* active enterprise development experience and a Fine art degree π¨
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.
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.
Pointers also allow you to keep references to multiple variables of the same type, kind of like an array, except with the help if
malloc
,calloc
, andrealloc
you can point to as many variables of the same type as you want, provided the memory still has space to allocate.Pointers enable you to have things like Strings in your C code (in the form of
char*
) since Strings arenβt native to C.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!
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.