Pointers.

Introduction to Pointers in C

Definition:

In C, a pointer is just a variable whose value is the address of another variable. We declare a pointer like this: data_type *variable_name.

Example:

/** declare a pointer */
int *ptr;

Here, we declared an integer pointer (ptr), which means ptr will store the address of an integer variable. To get the address of a variable, we use the &(ampersand) operator like this: &variable_name.

Example:

int p = 10;int *ptr = &p;

Here we are getting the address of p and storing it in the pointer ptr.

To retrieve the value stored at the memory location that is pointed by the pointer, use this: *pointer_name.

Example:

int p = 10;
int *ptr = &p;/** referencing */
/** dereferencing */
printf("%d\n", *ptr); /** prints 10 */
printf("%d\n", ptr);/** prints the address of p(for example 2048) */

Taking the address of an existing variable is called referencing and getting the value at an address by following the pointer(address) and getting the value is called dereferencing.

Why Pointers are Useful:

#include <stdio.h>void swap(int *p, int *r)
{
int temp = *p;
*p = *r;
*r = temp;
}
int main()
{
int a = 10, b = 20;
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
/** Output: a = 20, b = 10 */
}

What have we done?

  • swap function does not take integers here, Rather, two integer pointers i.e. address of two integer variables.
  • While calling the function swap in main, we are passing the addresses of variables a and b to it.
  • temp stores the value at the first address. We first go to the address that is stored in p, then get the value that is stored there and save it in variable temp. So now the temp is 10.
  • Then we go to the address of the first integer a and set its value to be the value of the second integerb Thus; We go to the address stored in r, get the value from there and save it in a’s address (remember that p is just the address of a and ris the address of b. So the value of a will now be 20.
  • Then we go to the address of the second integer b and set its value to be temp 10 So, now b is 10.

NOTE: When we declare a variable in our program, At execution time, a computer allocates some memory corresponding to this particular variable. How much memory it allocates depends upon the data type and upon the compiler.

Now, in a typical modern-day compiler,int = 4bytes, char = 1byte, float = 4bytes etc.

What is interesting about pointers, if you have a pointer to a value in the stack, For Example,char *c and int *a

*c;
*a;
*(c + 1) /** pointer moves 1 step or 1 byte for char character */
*(a + 1) /** pointer moves 1 step or 4bytes for int (integer) */

As soon as the computer sees a declaration during the program execution, it knows that this is an integer variable, so we need to allocate 4 bytes of memory, depending on the data_type of the pointer, it will allocate the memory for the same. The compiler would know how many memory allocations to move.

Till next time✌️, Happy coding 👩‍💻!

For more explanations, you can watch the youtube tutorial for the same, here

Finally, thanks for reading this article. I enjoyed writing it, and I hope you enjoyed reading it as well. Let me know what you think in the comments!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mildred Makori

Mildred Makori

A Software Engineer Student at ALX (Holberton) | Technical Writer