C Programming Data Pointer Syntax

Last modified by Microchip on 2023/11/09 09:06

Syntax


type *ptrName;
  • In the context of a declaration, the * merely indicates that the variable is a pointer.
  • type is the type of data the pointer may point to.
  • Pointer is usually described as “a pointer to type”.

Example

1 int *iPtr;        // Create a pointer to int
2
3 float *fPtr;      // Create a pointer to float

Initialization


To initialize a pointer, we need to set it equal to the address of another variable. We can achieve this with the & (address of) operator. Because we are setting the value of the pointer itself, we do not use the * operator here. We simply use the pointer's name, followed by an equals sign and the name of the variable whose address we want to be prefixed by the & operator.

p = &x;

In this example, we are setting the pointer p to the address of x, so now p points to x. Of course, this assumes that both p and x are declared as the same type (e.g. int).

Usage

Now that the pointer has been initialized, it may be used as if it were the variable it points to. To do this though, we need to prefix the pointer variable with the * (dereference) operator. Unlike in the pointer declaration, here the * means "use the value of the variable that p points to – not the value of p itself". Since we set p to point to x on the previous slide, {*p means the same thing as x in our code.

y = *p;

Another Way to Look at Syntax

1 int x, *p;     //1 int, 1 pointer to int
2
3 p = &x;        //Assign p the address of x
4 *p = 5;        //Same as x = 5;

Another way to look at the syntax that might help it make more sense is to consider the concept of a constant pointer and a variable pointer. First, understand that the terms address and pointer are synonymous. An address is a pointer.

So, in the code above, &x is a pointer to the variable x because & means "address of". Since x will always be at the same address (once it is assigned by the linker/compiler – it never moves), &x may be thought of as a constant pointer. Moreover, the value of &x is known at link time, so no computation needs to be done by the microcontroller. In every respect, this is a constant that happens to be a pointer (address).

The real power of pointers comes from the ability to have a variable pointer, where the address it points to may be changed any time. In the code above, p is a variable pointer. On the second line, we assign p, the constant value &x, so that p now points to x. We can modify the address that p points to in this fashion anywhere in our code.

After we have declared the pointer and given it an address to point to, we can now use it to access the data that resides at that address. To do this, we use the dereference operator *. Since p was assigned the address of x, anywhere we use *p, it is the same as if we used x directly. Therefore, on the last line, y = *p does the exact same thing as y = x.

Note that in the context of the pointer's declaration int *p, the * operator is only used to indicate that the variable p is a pointer to int rather than an ordinary variable of type int. It does not indicate indirection/dereference as it does when used in code.