Wednesday, January 17, 2024

Pointers Part 1: The Basics



So you're eager to learn about pointers but unfortunately you got stuck because they seemed to you terrible in nature? That's not true I know, but many of the people get confused when they arrive at the topic of pointers. Well pointers are the most important tools in C programming and are the one that can make you fly (unless you don't know how to ride over them). In this article we're going to learn basics of pointers.
Pointers are the varaibles that store addresses of other variables. Easy ain't it?
So lets start with the decleration of a pointer, pointer is decreleared as:
data_type *var_name;
e,g
int *pt;
well the astrisk(*) before the variable name is the thing that makes variable a pointer. So far so good now what?
Now lets say we want to store address of a variable in our pointer variable that seems pretty complex..!
Let's do it:
int number = 100;
int *pt = #
Is it really complex..?
what we are doing here is that we are first declaring and initializing a integer variable (number) with value of 100 and then we declare and initialize a pointer variable (pt) with the address of number variable. Now pt (pointer variable) contains the address of number (integer varaible). So what? Now we can use this pointer variable to change the value of number variable. Is this some kind of Magic? Maybe. Lets' do it:
*pt = 200;
what we have done here is that we De-referencing the pt variable with the asterisk (*) and then assigned it the value of 200 now the number variable contains 200. Isn't it a magic? De-referencing is used for accessing the value of the variable towards which our pointer is pointing simple. So lets write a full program of what we have learned so far.
/*Pointer Basics: Creating and Using Pointers*/
#include<stdio.h>
int main(void){
  int number = 100;
  int *pt = &number;
  printf("Value of 'number' is: %d", number);
  printf("Address of 'number' is: %p", pt);
  *pt = 200;
  printf("New value of 'number' is: %d", number);
  return 0;
}
What this whole program did was it created a integer variable and a pointer to integer variable and then printed out the value and address of the 'number' variable and after that we De-referenced the pointer variable so that we can access the value to which our pointer variable is pointing and changed the old 100 value with new 200 value and at last we printed that out. Easy isn't it?
But do you know that you can get the address of a variable even by using ampersand (&) operator? Lemme show you how. I'll declare and initialize a variable 'var' and then print it to screen using ampersand (&) operator:
int var = 10;
printf("Address of 'var' is %p\n", &var);
the last statement here will print out the address of 'var' not value so that means it is equal to this statement:
int *pt = &var;
printf("Address of 'var' is %p\n", pt);
here we first assigned the address of 'var' to pointer variable 'pt' and then printed out the address of 'var' using the pointer variable (pt).
So lets write another program that will wrap up this part of 'Pointer Basics':
/*Pointer Basics Part 1: Program 2*/
#include<stdio.h>
int main(void){
   int var = 10;
   int *pt = &var;
   printf("The Value of 'var' is: %d\n", var);
   printf("De-referencing: *pt = %d\n", *pt);
   printf("Ampersand: The Address of 'var' is %p\n",  &var);
   printf("pt = %p\n", pt);
   return 0;
}
So that's the end of first part watch out for the next part in which we'll tighten our grip on pointers and get ready for some Advanced '*po(inter)-fo'.
Related posts
  1. Hacker Tools For Mac
  2. Hacker Tools 2019
  3. Hacker
  4. What Are Hacking Tools
  5. Hacking Tools For Windows 7
  6. Hacker Tools Software
  7. Pentest Tools Subdomain
  8. Growth Hacker Tools
  9. Hacking Apps
  10. Hack Tools Mac
  11. Hacker Tool Kit
  12. Hacking Tools For Beginners
  13. Hacking Tools For Mac
  14. Hacks And Tools
  15. Hacking Tools Windows
  16. Hacking Tools For Games
  17. Hacker
  18. Hacker Tools Apk Download
  19. Kik Hack Tools
  20. Hacking Tools And Software
  21. Hacking Tools For Games
  22. Hacker Techniques Tools And Incident Handling
  23. Android Hack Tools Github
  24. Hacks And Tools
  25. Bluetooth Hacking Tools Kali
  26. Best Pentesting Tools 2018
  27. Pentest Tools Review
  28. Usb Pentest Tools
  29. Pentest Tools Android
  30. Kik Hack Tools
  31. Hacker Search Tools
  32. Pentest Tools Find Subdomains
  33. New Hacker Tools
  34. What Is Hacking Tools
  35. Pentest Tools Subdomain
  36. Hacking Tools For Windows 7
  37. Pentest Tools Find Subdomains
  38. Pentest Tools Android
  39. Hack App
  40. Hack Tools For Mac
  41. Hacker Tools List
  42. Pentest Tools Port Scanner
  43. Pentest Tools Url Fuzzer
  44. How To Install Pentest Tools In Ubuntu
  45. Pentest Reporting Tools
  46. Pentest Tools Free
  47. Hack And Tools
  48. Pentest Tools Tcp Port Scanner
  49. Hacker Tools For Pc
  50. Hack Website Online Tool
  51. Hackrf Tools
  52. Install Pentest Tools Ubuntu
  53. Hack Tools For Mac
  54. Nsa Hack Tools

No comments:

Post a Comment