

Declare and initialize an int variable named Result It also demonstrates how the indirection, and reference operators can be used in a basic form. The next example builds on the previous code, by using similar named variables. The variable Result has been initialized with a value of 10 (this has been done to avoid some random value being displayed), The value of P_Name is the memory address location of Result, and *P_Name has the same value as Result. The image below shows a screen capture from Eclipse. & or reference operator assigns the address of Result to P_Name int = the type of variable being declared The next example demonstrates a similar initialization, but the pointer and integer declarations have been added as well. When placed before the name of a variable, the reference operator returns the address of the variable. The ampersand is placed before the variable we want the pointer to point to. & or reference operator assigns the address of Variable_Name to The ampersand ( &) or Reference operator is used to do this, as it returns the address of the variable An example below shows how the pointer is initialized. This is carried out by the pointer storing the memory address, of the variable it points to. Initializing the pointer basically ensures the pointer actually points to something. Int *P_Name, Result Initializing Pointers Result standard integer variable being declared *P_Name pointer name which points to an integer Pointers can also be declared along with other non pointer variables, an example can be seen below. It is important to note that these are pointers and not variables, the compiler knows this due to the indirection operator, and the context in which it has been used. So the indirection operator tells the compiler which type of variable the pointer name points to, in this case either an integer or a char. The indirection operator tells the compiler P_Name1 is a pointer to an integer and P_Name2 is a pointer to a char. The next part which starts with an asterisk ( *) is known as the Indirection operator.

C indirection code#
So the first part of the code int and char in this case, is the type of variable the pointer points to. *P_Name2 indirection operator and pointer name char = the type of variable the pointer points to *P_Name1 indirection operator and pointer name int = the type of variable the pointer points to
C indirection how to#
The code snippet below shows how to declare a basic pointer, there are 2 examples showing an int and a char variable. When pointers are used with functions, it allows the values of variables to be changed regardless of where they originated. They are an important part of the C language, providing a powerful and flexible method of manipulating data. This first tutorial will be a basic introduction to C programming Pointers.
