Yes, today an another tutorial on C. Actually this is an advanced concept in C. However you can't understand some basics without learn pointers. If you read my tutorials on stack, exploit writing etc , I think the term pointer is nothing to you. You saw that there were many pointers such as ESP,EIP etc. ESP points to the top of the stack while EIP points to the address of next instruction that's waiting to get executed by CPU. When we talk about languages there is a term called pointers. They point to a memory address. To work with a pointer we have to do two steps. First we have to declare a pointer . This procedure is very similar to declaring a variable in C. How do you make a new integer variable? You may do that as int var1. (Where var1 is the name of variable) .
int *ptr;
hear we see an additional asterisk mark before ptr. That indicates this is not a integer variable and this is a pointer. An integer pointer should only point to an in tiger variable. If not the result will be different. Now first part of pointer is over. We declared it. But before we use it we have to initialize it so it'll point to somewhere. Assume ,we have a variable that in integer called int var. You know that somewhere in memory there is a space allocated for this variable. Since it an integer there should be 4 bytes for it. We can get the address of that variable by using a C command called "address of" . We use "&" sign to indicate that operation. So we can give that value (address) to our pointer like following.
ptr = &var;
Now ptr pointer points to var variable. What does it mean? ptr holds the address of var variable. But hear you may see some unclear situation. The integer variable is 4 byte length. So it have 4 different address. So which address is pointed by ptr. It is first byte's memory address. Now we have declared a pointer and we gave it a memory address to point. Now we can use this pointer. Let's see what we can do with it. 1) we can get the address of variable.
#include<stdio.h>
int main(){
  int var = 100;
  int *ptr;
  ptr = &var;
  printf("memory address of var is %p\n", ptr);
  return 0;
}
example-1-of-pointer If you want to know how to compile a c program take a look at compiling C programs tutorial There is another way to do this using "address of " operator.
printf("memory address of var is ?p\n" , &var) ;
2) we can get what inside of variable (The value of var) If we use a star mark in front of our pointer name we can get the value of variable instead of it's address.
#include<stdio.h>
int main(){
  int var = 100;
  int *ptr;
  ptr = &var;
  printf("Value of var is %d\n", *ptr);
  return 0;
}
example2-of-pointers-in-C You may ask why we use a pointer to get the value of variable? We can just use
printf("Value of var is ?d\n", var);
Yes of course. We can do it in that way. Actually this feature of a pointer is not helpful too much when we think about integers. When we discuss more about pointers I'll explain about this. OK. You learned basics of pointers in C language. There are lot of things to learn in this topic. I'll post them in separate tutorials. Thanks for reading.