
As any other variable, you need to declare a pointer. Furthermore, you have to initialize a pointer before you can use it.
You declare a pointer just like any other variable:
Syntax
type *name;
type can be any C type (such as char, int, or float for example).
name is the name of the pointer variable.
The * makes the variable a pointer.
Pointers are usually described as “a pointer to type”
Code example
int *myPointer // declares a pointer named myPointer as an integer type
float *myOtherPointer // declares a pointer named myOtherPointer as a float type
myPointer = &myIntVar; // assigns the memory address of myIntVar to myPointer
myOtherPointer = &myFloatVar; // assigns the memory address of myFloatVar to myOtherPointer
Syntax
typedef type *typeName;
Note
typeName which is a synonym for type.typeName explicitly identifies the variable as a pointer to type.Code example
typedef int *intPointer; // Create a pointer to int type
intPointer p; // Create pointer to int (without *)
// Equivalent to int *p;
To set a pointer to point to another variable, we use the & operator (address of), and the pointer variable is used
without the dereference operator *:
p = &n;
This assigns the address of the variable n to the pointer p (p now points to n).
Note
p must be declared to point to the type of n (e.g. int n; int *p;).When accessing the variable pointed to by a pointer, we use the pointer with the dereference operator *:
m = *p;
This assigns to the variable m, the value of what p is pointing to (n from the last example). Using *p, is the same as using the variable it points to (e.g. n).
Code example
int n, *p; // Create an integer variable and a pointer to an integer variable
p = &n; // Assign the address of n to p
*p = 3; // Equals to n = 3;
In the pointer world:
&n is a constant pointer which represents the address of n. This address of n will never change.p is a variable pointer to int which can be assigned the address of any int any time.*p represents the data pointed to by p and may be used anywhere you would use n.* is the dereference operator, also called the indirection operator.* is used to indicate that the variable is a pointer rather than an ordinary variable.Consider the following code example. How is this working in an actual design?
Code example
int n, m; // create two integer variables
int *p; // create a pointer to an integer variable
n = 0x1234; // assign 0x1234 to n
m = 0xABCD; // assign 0xABCD to m
p = &n; // assign the address of n to p
*p = 0x4321; // assign 0x4321 to n using *p
p = &m; // assign the address of m to p
*p = 0xDCBA; // assign 0xDCBA to m using *p
Zero-initialized RAM at program start:

Assign 0x1234 to n:

Assign 0xABCD to m:

Assign the address of n to p:

Assign 0x4321 to n using *p:

Assign the address of m to p:

Assign 0xDCBA to m using *p:
