SRM GROUP OF INSTITUTIONS
RAMAPURAM CAMPUS
Pointers
Developed by
Ms.Preethy Jemima
AP/CSE , SRMIST
Definition
A pointer is a variable whose value is the address of another variable, i.e.,
direct address of the memory location.
 Double (**) is used to denote the double pointer.
 Double Pointer Stores the address of the Pointer
Variable.
int main()
{ int v = 76;
int *p1;
int **p2;
p1 = &v;
p2 = &p1;
printf("Value of v = %dn", v);
printf("Value of v using single pointer = %dn", *p1 );
printf("Value of v using double pointer = %dn", **p2);
return 0; }
Output
Value of v = 76
Value of v using single pointer = 76
Value of v using double pointer = 76
 A pointer in c is an address, which is a numeric value.
Therefore, you can perform arithmetic operations on a pointer
just as you can on a numeric value.
 There are four arithmetic operators that can be used on
pointers: ++, --, +, and -
 Eg: ptr is an integer pointer which points to the address 1000.
Assuming 32-bit integers. Perform ptr++.
 After the above operation, the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current
location.
 This operation will move the pointer to the next memory
location without impacting the actual value at the memory
location.
Note:
 If ptr points to a character whose address is 1000, then the
above operation will point to the location 1001 because the
next character will be available at 1001.
 The size of void pointer varies system to system.
 If the system is 16-bit, size of void pointer is 2 bytes. If the
system is 32-bit, size of void pointer is 4 bytes. If the
system is 64-bit, size of void pointer is 8 bytes.
Example
#include <stdio.h>
int main() {
void *ptr;
printf("The size of pointer value : %d", sizeof(ptr));
return 0; }
Output
The size of pointer value : 8
#include <stdio.h> //Example for pointer arithmetic incrementing
const int MAX = 3;
int main () {
int var[] = {10, 100, 200};
int i, *ptr;
ptr =&var;
for ( i = 0; i < MAX; i++)
{ printf("Address of var[%d] = %xn", i, ptr ); printf("Value
of var[%d] = %dn", i, *ptr );
/* move to the next location */
ptr++;
}
return 0; }
The %x is the printf format that indicates that the int value
should be displayed in hexadecimal.
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200
#include <stdio.h> //Example for pointer arithmetic decrementing
const int MAX = 3;
int main ()
{ int var[] = {10, 100, 200};
int i, *ptr;
ptr = &var[MAX-1];
for ( i = MAX; i > 0; i--)
{
printf("Address of var[%d] = %xn", i-1, ptr ); printf("Value
of var[%d] = %dn", i-1, *ptr ); ptr--; }
return 0; }
Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
Value of var[0] = 10
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to addn");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of the numbers = %dn", sum);
return 0;
}
 So X [0] [0] and **X are also identical. To index by
anything other than 0, you have to bracket the
precedence.
 For example, X [1] [3] is identical to:
*(*(arr + 1) + 3); that’s not too easy to read.
 When you assign to int **ptr = (int **) arr; the value
assigned is the address of the array.
 You have told the compiler that ptr is to be treated as a
pointer to a pointer to an int (which it is not), and to stop
it warning you, you have also cast the value.
 Remember a cast does not change the bits copied – it
just tells the compiler to accept the copy without arguing.
 In the following example we are creating a
string str using char character array of size 6.
 char str[6] = "Hello";
 The above string can be represented in memory as
follows.
 Each character in the string str takes 1 byte of memory
space.
 The variable name of the string str holds the address of
the first element of the array i.e., it points at the starting
memory address.
 So, we can create a character pointer ptr and store the
address of the string str variable in it. This way, ptr will
point at the string str.
char *ptr = str;
#include <stdio.h>
int main(void) {
char str[6] = "Hello"; // string variable
char *ptr = str; // pointer variable
while(*ptr != '0') // print the string
{
printf("%c", *ptr);
ptr++; // move the ptr pointer to the next memory location
}
return 0;
}
We can achieve the same result by creating a character pointer that points at
a string value stored at some memory location.
In the following example we are using character pointer variable strPtr to
store string value.
#include <stdio.h>
int main(void)
{
char *strPtr = "Hello"; // pointer variable to store string
char *t = strPtr; // temporary pointer variable
while(*t != '0') // print the string
{
printf("%c", *t); // move the t pointer to the next memory location
t++; }
return 0;}
Note!
In the above code we are using another character pointer t to print the
characters of the string as because we don't want to lose the starting
In the above image the string "Hello" is saved in the memory
location 5000 to 5005.
 The pointer variable strPtr is at memory location 8000 and is
pointing at the string address 5000.
 The temporary variable is also assigned the address of the
string so, it too holds the value 5000 and points at the starting
memory location of the string "Hello".
 A function pointer, also called a subroutine
pointer or procedure pointer, is a pointer that points
to a function.
 As opposed to referencing a data value, a function
pointer points to executable code within memory.
 Dereferencing the function pointer yields the
referenced function, which can be invoked and passed
arguments just as in a normal function call.
 Such an invocation is also known as an "indirect" call,
because the function is being
invoked indirectly through a variable instead
of directly through a fixed identifier or address.

Pointers

  • 1.
    SRM GROUP OFINSTITUTIONS RAMAPURAM CAMPUS Pointers Developed by Ms.Preethy Jemima AP/CSE , SRMIST
  • 2.
    Definition A pointer isa variable whose value is the address of another variable, i.e., direct address of the memory location.
  • 4.
     Double (**)is used to denote the double pointer.  Double Pointer Stores the address of the Pointer Variable.
  • 5.
    int main() { intv = 76; int *p1; int **p2; p1 = &v; p2 = &p1; printf("Value of v = %dn", v); printf("Value of v using single pointer = %dn", *p1 ); printf("Value of v using double pointer = %dn", **p2); return 0; } Output Value of v = 76 Value of v using single pointer = 76 Value of v using double pointer = 76
  • 6.
     A pointerin c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value.  There are four arithmetic operators that can be used on pointers: ++, --, +, and -  Eg: ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers. Perform ptr++.  After the above operation, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer location which is 4 bytes next to the current location.  This operation will move the pointer to the next memory location without impacting the actual value at the memory location. Note:  If ptr points to a character whose address is 1000, then the above operation will point to the location 1001 because the next character will be available at 1001.
  • 7.
     The sizeof void pointer varies system to system.  If the system is 16-bit, size of void pointer is 2 bytes. If the system is 32-bit, size of void pointer is 4 bytes. If the system is 64-bit, size of void pointer is 8 bytes. Example #include <stdio.h> int main() { void *ptr; printf("The size of pointer value : %d", sizeof(ptr)); return 0; } Output The size of pointer value : 8
  • 8.
    #include <stdio.h> //Examplefor pointer arithmetic incrementing const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; ptr =&var; for ( i = 0; i < MAX; i++) { printf("Address of var[%d] = %xn", i, ptr ); printf("Value of var[%d] = %dn", i, *ptr ); /* move to the next location */ ptr++; } return 0; } The %x is the printf format that indicates that the int value should be displayed in hexadecimal.
  • 9.
    Address of var[0]= bf882b30 Value of var[0] = 10 Address of var[1] = bf882b34 Value of var[1] = 100 Address of var[2] = bf882b38 Value of var[2] = 200
  • 10.
    #include <stdio.h> //Examplefor pointer arithmetic decrementing const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; ptr = &var[MAX-1]; for ( i = MAX; i > 0; i--) { printf("Address of var[%d] = %xn", i-1, ptr ); printf("Value of var[%d] = %dn", i-1, *ptr ); ptr--; } return 0; }
  • 11.
    Address of var[2]= bfedbcd8 Value of var[2] = 200 Address of var[1] = bfedbcd4 Value of var[1] = 100 Address of var[0] = bfedbcd0 Value of var[0] = 10
  • 12.
    #include <stdio.h> int main() { intfirst, second, *p, *q, sum; printf("Enter two integers to addn"); scanf("%d%d", &first, &second); p = &first; q = &second; sum = *p + *q; printf("Sum of the numbers = %dn", sum); return 0; }
  • 13.
     So X[0] [0] and **X are also identical. To index by anything other than 0, you have to bracket the precedence.  For example, X [1] [3] is identical to: *(*(arr + 1) + 3); that’s not too easy to read.  When you assign to int **ptr = (int **) arr; the value assigned is the address of the array.  You have told the compiler that ptr is to be treated as a pointer to a pointer to an int (which it is not), and to stop it warning you, you have also cast the value.  Remember a cast does not change the bits copied – it just tells the compiler to accept the copy without arguing.
  • 14.
     In thefollowing example we are creating a string str using char character array of size 6.  char str[6] = "Hello";  The above string can be represented in memory as follows.  Each character in the string str takes 1 byte of memory space.
  • 15.
     The variablename of the string str holds the address of the first element of the array i.e., it points at the starting memory address.  So, we can create a character pointer ptr and store the address of the string str variable in it. This way, ptr will point at the string str. char *ptr = str;
  • 16.
    #include <stdio.h> int main(void){ char str[6] = "Hello"; // string variable char *ptr = str; // pointer variable while(*ptr != '0') // print the string { printf("%c", *ptr); ptr++; // move the ptr pointer to the next memory location } return 0; }
  • 17.
    We can achievethe same result by creating a character pointer that points at a string value stored at some memory location. In the following example we are using character pointer variable strPtr to store string value. #include <stdio.h> int main(void) { char *strPtr = "Hello"; // pointer variable to store string char *t = strPtr; // temporary pointer variable while(*t != '0') // print the string { printf("%c", *t); // move the t pointer to the next memory location t++; } return 0;} Note! In the above code we are using another character pointer t to print the characters of the string as because we don't want to lose the starting
  • 18.
    In the aboveimage the string "Hello" is saved in the memory location 5000 to 5005.  The pointer variable strPtr is at memory location 8000 and is pointing at the string address 5000.  The temporary variable is also assigned the address of the string so, it too holds the value 5000 and points at the starting memory location of the string "Hello".
  • 21.
     A functionpointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function.  As opposed to referencing a data value, a function pointer points to executable code within memory.  Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call.  Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed identifier or address.