[C] Pointers – Overview

Pointers are the heart of C language and also evil in programming in general. Why pointers are bad and other new languages (Java or C#) try to remove them?

 

1. Pointers

Pointers are variables that have the memory address in them. So the size of a pointer variable is nothing to do with the underlying data types. It is the size of the memory address (4 bytes for 32 bit machines and 8 bytes for 64 bit machines).

In C, pointers are essential when passing values as references. The problem arises because programmers are responsible for any misues or mistakes of using memory addresses. Any mistake of pointers results in an application crashing.

C# and Java move this responsibility from programmers to compilers. By using references, pointing memory addresses can be achieved indirectly.

In most cases, references are preferable to pointers. But when you are using C, you need to use pointers. Don’t worry. If you are a little more careful, using pointers is not hard.

[Note] Don’t be fooled by some weird syntaxes such as “***p” (pointer to the pointer to the pointer). Just stick to the most basic concept and it will be OK.

 

2. Pointer Operators

There are 2 operators for pointers:

  • &“: finds the address of a variable
  • *“: points to the content of a memory address (read / write)
int i = 10;
int *p = &i;

printf("The address of the int variable: %p n", &i);
printf("The value of the int variable: %i n", i);
printf("The address value of the pointer variable: %p n", p);
printf("The content value to which the pointer variable points: %i n", *p);

 

3. Passing By Reference

By default, when C passes a value as an argument to function C copies it to a new variable. (Pass by Value).

void AddOne(int i)
{
  i++;
}

void PointerTest1()
{
  int i = 10;
  printf("i = %i n", i);
  AddOne(i);
  printf("i = %i n", i);
}

The code prints 10 even after the “AddOne” function call.

void AddOneByPointer(int *i)
{
  (*i)++;
}

void PointerTest2()
{
  int i = 10;
  printf("i = %i n", i);
  AddOneByPointer(&i);
  printf("i = %i n", i);
}

This code increase the value of  “i” by one.

 

4. struct and Pointers

The “struct” type is a main custom data type in C. So it is important to know how to use pointers with struct types.

The basic idea is the same as primitive types. When you point to a struct variable, it points to the address of the content. But you need to know how to access each member of a struct type.

The first syntax is using the “*” operator.

(*p).member;

It works fine but C provides a liitle bit clearer syntax.

p->memeber;

typedef struct
{
  char name[40];
  int age;
}
Person;

void PointerTest3()
{
  Person homer = { "Homer Simpson", 40 };
  Person child = { "Bart Simpson", 10 };
  Person *bart = &child;

  printf("Name = %s, Age = %i n", homer.name, homer.age);
  printf("Name = %s, Age = %i n", (*bart).name, (*bart).age);
  printf("Name = %s, Age = %i n", bart->name, bart->age);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s