2. Variable and data type:
ď A "normal variable" is a location in memory that can
hold a value.
ď It is nothing but the naming convention to refer the
address in which the value gets stored.
ď Data type determines the no of bytes allocated to the
data.
Memory location:
ď Each memory location is identified and referenced with an
address.
ď The address in which the data gets stored.
ď For int data type, two bytes get allocated. The starting byteâs
address will be returned as the address of the variable.
3. The & and * operators:
Both are unary operators
& - Address of operator
- It returns the address of the variable
* - Indirection (or) dereference (or) Value at address operator
main()
{
int a;
a=2;
printf(âAddress of a %uâ, &a);
printf(âValue at address of a %dâ, *(&a));
}
Output:
Address of a 65435
Value at address of a 2
4. ď The operator * should not be used in front of the variable.
ď It has to be used in front of address of the variable to retrieve
the value.
Variable Name Value at the address Address of the variable
a 2 65435
main()
{
int a;
a=2;
printf(âValue of a %dâ, *a);
}
Already âaâ is holding the value only.
Again applying * to âaâ will throw the
error.
5. Pointer Variable:
The variable which holds the address. A pointer is a variable that
points to another variable. A pointer "points to" that other variable
by holding a copy of its address. In the simplest term pointer is a
nearly integer variable which stores a memory address.
Declaration:
Syntax:
data type * pointervariableName;
data type â Refers to the data type of the value that is stored in the
address the pointer points to. It can be of any type (i.e) int, float,
char, struct etc.,
* - Denotes the declared variable is a pointer. It is used to differentiate
the normal variable from the pointer variable.
PointervariableName â The naming conventions applicable to the
normal variable applies here also.
Caution : * operator is also used to declare the pointer variable.
6. Normal Variable declaration : char a;
Pointer Variable declaration : char *a, *b;
In the above declaration,
Char â The value stored in the address the pointer holds is of char data
type
a - Pointer variable name;
ďź More than one pointer variable can be declared in the same line. All
of same type.
Why pointer variable is declared with data type?
Even though the pointer is just an integer, the data type is used to
determine the type of data it points to. So that it can retrieve the
value stored in the address, the pointer points to.
Note:
ď The type of variable the pointer is pointing to decides pointers
properties and behavior.
ď In the declaration, data type * will give the variable the special
attention that it is a pointer variable.
ď To make an ordinary variable to be a pointer variable, * is added
between the data type and variable name.
7. Initialization of Pointer variable:
ď Assigning value to the pointer variable. (i.e) address.
ď & in front of a variable will retrieve the address of the variable.
main()
{
int a;
a=2;
printf(âAddress of a %uâ, &a);
printf(âValue at address of a %dâ, *(&a));
}
Output:
Address of a 65435
Value at address of a 2
main()
{
int a;
printf(âAddress of a %uâ, p);
printf(âValue at address of a %dâ, *p);
}
Output:
Address of a 65435
Value at address of a 2
int *p;
p=&a; int *p=&a;
a=2; *p=2;
8. Variable Type Value Address
a int 2 65435
&a - 65435 -
p int * 65435 65432
&p - 65432 -
*p int 2 -
*(&a) int 2 -
Variable Type Value Address
a Int 3 65435
&a - 65435 -
p Int * 65435 65432
&p - 65432 -
*p int 3 -
*(&a) int 3 -
10. ď Pointer variable can be assigned to another pointer variable.
int *r, *k, h;
r=&h;
k=r;
Now r, k both point to same location(i.e) address of h.
Uninitialized Pointer:
ď int *r, h;
printf(â%dâ,*r); // will result in error
ď Uninitialized pointers can point to any memory location.
ď Using * (indirection operator) on uninitialized pointers may result in
program throwing a run time error.
ď Using a pointer without initializing it to any data may result in data
corruption or even program crash.
ď Make sure you initialize all pointers to a valid address before
dereferencing them.
Pointer variable not assigned with any value(Address).
11. Invalid Pointer References:
ď An invalid pointer reference occurs when a pointer's value is
referenced even though the pointer doesn't point to a valid block.
ď p=q; when q is uninitialized. The pointer p will then become
uninitialized as well, and any reference to *p is an invalid pointer
reference.
Zero Pointer Reference:
ď A zero pointer reference occurs whenever a pointer pointing to zero is
used in a statement that attempts to reference a block. For example, if
p is a pointer to an integer, the following code is invalid:
int *p;
p = 0;
*p = 12;
ď There is no block pointed to by p. Therefore, trying to read or write
anything from or to that block is an invalid zero pointer reference.
ď Dereferencing such a pointer, however, is invalid.
12. ď The data type of the variable and the type of pointer which
holds the address of the variable must be same.
int r;
char *h, g;
h=&r; // Wrong
h=&g; // Correct
ď Do not initialize the pointer variable with normal values. Even
though it is an integer, it is used to hold the address value.
ď int *r, h;
r=2; // Wrong
*r=2; // Correct
13. main()
{
float age;
float *adAge;
adAge=&age;
printf(âNo of bytes of pointer variable
%dnâ, sizeof(adAge));
printf(âNo of bytes of value pointer
points to %dâ, sizeof(*adAge));
printf(âNo of bytes of variable %dâ,
sizeof((age));
}
Output:
No of bytes of pointer variable: 2
No of bytes of value pointer points to: 4
No of bytes of variable : 4
sizeof pointer variable:
Pointer variable always holds address
value only. Hence always the size will
be 2 bytes. This is platform dependent.
The size of pointer variable doesnât
depend on the data type of the value it
points to.
14. Null Pointer:
ď Null pointer is a pointer which points to nothing.
ď Pointer can be declared NULL if it cannot be initialized at the beginning and the
value is initialized during execution. Or assign zero to pointer variable.
ď When a pointer is declared, an uninitialized pointer is set to NULL.
ď It points to the base address of CPU register and since register is not
addressable, usage of a null pointer will lead to crash or at minimum a
segmentation fault. Depending on the compiler the value varies. Most of the
cases it will be zero.
main()
{
int *r, h;
r=NULL; // r=0;
printf(âAddress the pointer holds %u :â, r); // Compiler dependent
printf(âValue the pointer points to %d:â, *r); // Program crashes
}
15. void Pointer:
ď void pointer technically is a pointer which is pointing to the unknown.
ď Also in dynamic memory allocation function such as malloc ( ) and alloc ( )
returns void pointer which can be easily converted to other types.(Will dealt
later)
ď As the data the pointer points to is not known, dereferencing void pointer will
result in error.
main()
{
int h;
void *r;
r=&h;
printf(âsize of pointer variable %d :â, sizeof(r)); // Compiler dependent
printf(âsize of value pointer points to %d:â, sizeof(*r)); // Program crashes
}