Mar 08, 2022

C programming pointers explained

Pointer is a important concept in C programming. In this document we are going to learn about pointers and their usage.

Let's give your attention to following C program.

int main(){
  int var;
  int *ptr;
  var  = 100;

  ptr = &var;

  printf('Address of \'var\' is %p\n', &var);
  printf('Address of \'ptr\' is %p\n', &ptr);
  return 0;

In top of main function we declare an integer variable named "var" using the command int var;. What happen when we make a new variable? A new memory space will be allocated for our variable. You know an integer variable is four bytes long. That means it can hold maximum four bytes long data.

In next code line , you can see a command as int *ptr;. This will create a pointer to an integer variable. hear we see an additional asterisk mark before ptr. That indicates this is not a integer variable and this is a pointer. A pointer variable is used to hold a memory address. In 32 bits architecture a word size is 32 bits (4 bytes) long. So a memory address is 4 bytes long. So a pointer variable is also 4 bytes in size.

Bellow image shows the memory layout when we declare above two variables.

Both of "var" and "ptr" variables are declared inside the "main" function. So there are local variables. If you have read our previous articles about stack and functions you know local variables are declared on the stack. The stack is started from high memory and grows into lower memory. Since "var" is declared before "ptr" , "var" variable must be at the beginning of the stack and "ptr" variable is located after it. We can see this with memory addresses too. The address of "var" variable is "0xbffffcfc" and "ptr"'s address is "0xbffffcf8".

See that our integer variable var is starts at 0xbffffcfc and ends at 0xbffffcf8 . The four bytes from 0xbffffcf8 to 0xbffffcfc can be used for store a integer value.

Next ptr pointer is started at 0xbffffcf8 and ended at 0xbffffcf4 . So memory space from 0xbffffcf4 to 0xbffffcf8 can be used to store a memory address. Because we use a pointer variable to store a memory location.

You may wonder how I used these memory addresses. Actually I first ran above program before illustrate the layout.

Now let's see our C program again. As the next step the value 100 is assigned to our integer variable by using "var = 100;". What actually happen in mashing level is copy 100 into the allocated memory space for "int" variable. In following image you can see that.

OK. I think you got it. Now move forward. In above code you can see some operators like &var and &ptr. What the do?

In C language there is a special operator called "address of operator". This is used with variables and it returns the address of the specified variable. "The address of operator" is indicated by "&" symbol. Actually what this operator return is the starting point of the variable. That means the memory address of the first byte of variable. For an example our integer "var" variable is four bytes long. by using &var , we will get 0xbffffcfc .

You know a pointer is also a special kind of variable. It also has a memory space. So if we use &ptr , we will get 0xbffffcf8.

At this point our pointer variable holds nothing. (Actually we can't assume what inside of this pointer .Because we didn't copy anything to that. but it may holds some junk data.)

Next you can see a command as "ptr = &var;" . You may guess what it does. At right side it uses the "address of operator" to get the memory address of the "var" variable. Next it assign that value to the pointer. This will simply copy the memory address of "var" variable into "ptr" pointer.

Let's see the output of our program. I used GCC compiler to compile it.

[email protected]:/c# ./ptr
Address of 'var' is 0xbffffcfc
Address of 'ptr' is 0xbffffcf8

Excellent . It worked as expected. Now we have declared a pointer and assigned a memory address to it. Now we can see what we can do with a pointer.

Basic usage of a pointer

In C language there are two different ways to access data of a pointer. Those are direct access and indirect access. Let's take bellow example.

int main(){
  int var;
  int *ptr;
  var  = 100;

  ptr = &var;

  printf('Address of \'var\' is %p\n', ptr);
  printf('Value of \'ptr\' is %d\n', *ptr);
  return 0;

First part is same as above first code. Take a look at two printf functions. What they will print out?

In first printf function we use "ptr" as the argument. So we are getting the value of our ptr pointer. At the moment it is pointing to the var variable. That means pointer holds the memory address of integer variable. So by this way we are printing the memory address of var variable.

What about second printf function?. we give "*ptr" as the argument for that. Actually "*ptr" is not the value of pointer. This will get the memory address saved at pointer, go to that memory address and get the value found at that address. At the moment pointer is pointing to the var variable. So memory address of integer variable is in pointer. Therefore "*ptr" returns the value of var variable.

[email protected]:/c# ./ptr
Address of 'var' is 0xbffffcfc
Value of 'var' is 100

Can you guess what following codes parses return?

  • var : This will return the value of integer variable
  • &var : This will return the address of integer variable
  • &ptr : This will return the address of pointer variable
  • *ptr : This will return the value of integer variable

Arithmetic operations on pointers

we can do various arithmetic operations on normal variables.

Oct 16
Protostar Stack3 Tutorial

Hello there in this article we are going to see how we can exploit and win protostar stack 3 level.....

Aug 20
Assembly basic tutorial - add two numbers

Welcome guys, today another basic tutorial on assembly coding. Hear I'll explain you how we can add....

Jun 19
Debugging Binaries with GDB

GDB is shipped with the GNU toolset. It is a debugging tool used in Linux environments. The term....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.