Pointer in C
-
Pointer Syntax in C:
-
Characteristics of C Pointers
- Pointers are special variables that store the memory address of another variable.
- It stores the address but of same type.
- Pointers always hold addresses as a whole number.
- Pointers in C are always initialized to NULL. For example, int *ptr=null;
- Null pointer symbol is ''0''.
- The asterisk symbol, * is used to retrieve the value of the variable; & ampersand symbol is used for retrieving the address of a variable.
- If a C pointer is assigned to the null value, it points nothing.
- Only subtraction between pointers are allowed. Addition, division, and multiplication operations are not allowed.
- The memory size of a C pointer for a 16-bit system is 2 bytes. Or It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.
-
Pointer Notation
- Reserve space in memory to hold the integer value.
- Associate the name a with this memory location.
- Store the value 5 at this location.
- We may represent a’s location in memory by the following memory map.
What is Pointer ?
Pointer is a special variable that stores address/memory-location of other variable but of same type. An * (asterisk) symbol followed by the variable name is used to declare a variable as pointer. When we talk about the address of a variable, then we’ll be able to go to that address and retrieve the data stored in it.
datatype *variable_name; Example: you could declare a pointer that stores the address of an integer with the following syntax: int *points_to_integer; Notice the use of the *. This is the key to declaring a pointer; if we add it directly before the variable name, it will declare the variable to be a pointer.
Consider the declaration
int a = 5;
This declaration tells the C compiler to
We see that the computer has selected memory location 65539 as the place to store the value 3. The location number 65539 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 5. The important point is that , a’s address in memory is a number. We can print this address number through the following program:
#include<stdio.h>
void main()
{
int a = 5 ;
printf("\nAddress of a = %u",&a);
printf("\nValue of a = %d",a);
}
The output of the above program would be:
Address of a = 65539
Value of a = 5
Look at the first printf() statement carefully. & used in this statement is C’s "address of" operator. The expression &a returns the address of the variable a, which in this case happens to be 65539. Since 65539 represents an address, there is no question of a sign being associated with it. Hence it is printed out using %u, which is a format specifier for printing an unsigned integer. We have been using the & operator all the time in the scanf() statement.
The other pointer operator available in C is *, called "value at address" operator. It gives the value stored at a particular address. The "value at address" operator is also called "indirection" operator.
Observe carefully the output of the following program:
#include<stdio.h>
void main()
{
int a = 5 ;
printf("\nAddress of a = %u",&a);
printf("\nValue of a = %d",a);
printf("\nValue of a = %d",*(&a));
}
The output of the above program would be:
Address of a = 65539
Value of a = 5
Value of a = 5
Note that printing the value of *(&a) is same as printing the value of a. The expression &a gives the address of the variable a. Another example to get the address of a variable.
Example:
void main( )
{
int a = 5 ;
int *b ;
b = &a ;
printf ("\nAddress of a = %u",&a) ;
printf ("\nAddress of a = %u",b) ;
printf ("\nAddress of b = %u",&b) ;
printf ("\nValue of b = %u",b) ;
printf ("\nValue of a = %d",a) ;
printf ("\nValue of a = %d",*(&a)) ;
printf ("\nValue of a = %d",*b) ;
}
The output of the above program would be:
Address of a = 65539
Address of a = 65539
Address of b = 65518
Value of b = 65539
Value of a = 5
Value of a = 5
Value of a = 5
Here
b = &a ;
But remember that b is not an ordinary variable like any other integer variable. It is a variable that contains the address of other variable . Since b is a variable the compiler must provide it space in the memory. Once again, the following memory map would illustrate the contents of a and b.