Hello everyone! Hope y'all are doing great. This post will continuing of the previous programming tutorial series, so lets begin! This tutorial will be about pointers. So, in programming, you will see something called pointers, quite alot of times actually. Escpecially in low-level programming, you will be required to eventually use them. What is a pointer? Pointer in programming, shortly said, is like piece of memory that points to another point in memory.. How does it look in reality? Lets say we have imaginary 4bit computer, with the maximum alocatable memory, thus 16 4bit integers, with maxiumum value of 16(computer architecutre controls the maximum memory size, as well as maximum integer capacity). It looks something like this: |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15| |0|0|0|0|0|0|0|0|0|0| 0| 0| 0| 0| 0| 0| as you can see the memory goes from zero to fifteen, making total of 16avilable slots. Now, lets say we place numbers 1 and 2 respectively in fiels 3 and 5. the memory will now look like this: |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15| |0|0|0|1|0|2|0|0|0|0| 0| 0| 0| 0| 0| 0| As you can see, the rest of the memory is still all zeroes out. Now, how do we make a pointer? Well, we simply imagine that we want to create a pointer to the field of index 3, in the field 15. Lets represent it with some code, shall we? u4 memory[16]; memory[3] = 1; memory[5] = 2; memory[15] = &memory[3]; (As you can notice, we're using & sign for pointers, which is also sign used for various things such as and operation in conditions, etc.) Right now, the memory[15], will contain the pointer to the memory[3]. We can also use simple math to edit the pointer or anything similiar, such as this: memory[15] = memory + 3; This will archieve same purpose, because the memory is an array, meaning its also (kindof) a pointer. Using this, you must first ensure you are working with the capacity with the size of 1, for example on normal 32/64bit computers the int32 or int64 can take 32bits(or 4 bytes) and int64, logically, will take 64bit, aka 8bytes. So in that case you'd have to do memory + 3*8; or memory + 3 * sizeof(long long); to make it more dynamic. So now with that out of the way, lets look at the memory model: |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15| |0|0|0|1|0|2|0|0|0|0| 0| 0| 0| 0| 0| 3| As you can see, nothing changed with the original value, but the memory at index 15 now contains the value 3, also currently the memory index of the thing its pointing at. This is same for modern computers too, but since the memory can take up to multiple gigabytes, the pointer numbers will be very very large. For example when you now print the memory[15], it will contain 3. Now, what would I be returned? Since I have 8gigabytes of memory with about 4gigs of it used, we can expect the value to be at about 4gigabytes of memory. If we represent 4gigabytes in hex, this number will be outputed, 0x100000000. When I tried to print out some pointer, it was a bit below that number, because the memory is dynamically being allocated and deallocated every few miliseconds. So the pointer was 0x4487f67c, which is about what could be expected. The value is obviously represented in hex(base16), because its much easier to work with when using base2, because base16 has 16 types of digits, which is also power of 2. So now that you understand pointers, what can we use pointers for? Pointers can be used for big amount of usecases, but they become absolutely required in low level languages like C or assembly. For example, when printing hello world, do you think we use pointers? Well, lets write a simple program and test that theory: char msg[] = "Hello, world!"; printf("%s", msg); Do you think we've used any pointers here? Yes, actually, we used 1 pointer. While transporting the Hello world msg array to printf, we used a pointer, because it would be inefficient to transport the entire array by copying it somewhere else, we just take the things address, and done! Now, what is a disadvantage of pointers? The main is, your program will crash if you screw it up. For example, lets make a simple program, that tried to read a value out of bounds: int array[16] = {0, 1, ...}; printf("%d", array[16]); As you can see, I've tried to print a value at the index 16, which is indeed out of the array. In this case, we will most likely get a segfault, aka segmentation fault. This happens when the memory you are trying to access is out of bounds of your programs permissions. The thing is, the kernel is deciding what program will take which memory segment. If the segment shouldn't be accessed by that program, the kernel will deny it resulting in so called segfault. This is also the most common Clang error, indicating its unsafe nature. How does pointer code look on the most low level code, assembly? Lets do some pointer code! section .bss msg resb 100 section .text mov eax, msg ; grab the pointer of msg mov ebx, [eax] ; grab the value of msg[0], because its pointer is stored inside eax As you can see here, I am allocating 100bytes to the array msg with all the bytes unset. The in the text section, where the code itself begins, I am grabbing the memory address of msg and putting it into eax. EAX is now equal to the pointer address of msg(EAX = msg + 0). After that, I am moving the value stored inside the msg which eax points to to ebx, using the [] syntax(all of this is nasm assembly). So, pointers are useful for giving some function arrays, etc. but what else? Well, for example when you want to return multiple values from a function, you can do all the things you want to return as pointer arguments, and use that as it. int function(int input1, int input2, int* out1, int* out2){ *out1 = input1 + input2; *out2 = input1 * input2; return input1 - input2; } and just like this, pointers can also be used. Now lets talk about casting pointers. Personally, I find that the quickiest way to convert for example 20bytes to 5 32bit integers, is using a simple pointer conversion. How does it work? char str[20] = {10, 20, 30, 19 ...}; // converts it to integer pointer int *strInt = (int*)str; strInt[1] == firstInteger! And thats propably all for today! See you... soon!