Overview
- Pointers allow us to access and modify contents of the RAM
- Powerful but dangerous feature
- Allows writing efficient code with hardware accessibility
Definition of Pointer
- Every variable and function in a program has a particular address
- Address = Position of memory location where data is stored
- Number of bytes allotted to each data item depends upon its type
- Pointer is a variable that represents address of a data item stored in memory.
- The variable is called pointer because it points to location where data item is stored in memory.
Advantages of using Pointers OR When to use Pointers
- Enables us to access a variable that is defined outside the function
- Pointers are more efficient is handling data tables
- Pointers reduce length and complexity of the program
- Increase execution speed
- Use of pointers to char strings results in saving of data storage space in memory
- Used for implementing data structures like linked lists, trees, graphs.
Address (&) operator
- Used to get address of a variable.
- As used in scanf() to store the read values at given address
- Remember:
- Address is constant while pointer is a variable
- Pointer is NOT address but a variable to store address of another variable.
Declaring Pointers
- Declaring a pointer variable means giving information to compiler about a pointer:
- Name of pointer variable
- Data type of pointer variable
- Syntax: data_type *ptr [,*ptr1,..,*ptrn];
- Content at address pointer should correspond to respective data type.
Initializing pointers
int i
int *pi;
pi = &i
OR,
int i
int *pi = &i
- Variable whose address is assigned should be declared first
- Initialization prevents computer using garbage values (i.e. prior contents) in memory.
Printing Address in Pointer Variable
int i;
int *pi = &i
printf(“Address of i=%x\n”,&i);
printf(“This is same as=%x\n”,pi);
Indirection Operator (*)
- Used to get value contained in memory location pointed by pointer variable
int *pi = &i
printf(“Value of i=%d\n”,i);
printf(“This is same as=%x\n”,*pi);
Pointer Arithmetic
- Indirection (*) operator has higher precedence then + or -
*pi = *pj + x;
*pj = *pi - *pj;
- Only + and – are allowed for pointer variables. * and / are meaningless and invalid for using with address.
- Subtly, pj * pi is meaningless but not *pi * *pj;
Pointers & Arrays
int x[5] = {11,22,33,44,55};
int *p = x;
- Array name is actually address of its first element. i.e.
- Since x (array name) is address, so no need to use '&' for getting address
- Since string is array of char, '&' is not used while reading string using scanf.
printf("Value pointed by p = %d\n",*p);
printf("This is same as =%d\n",x[0]);
Pointer Increment
- Used for accessing other elements of an array
- '++' helps you to increment value of a pointer by size of data type it points to. i.e.
- p++ => p = 1000 + 2 * 1 = 1002 i.e. address of second element which is x[1] = 22
- *p++ => ++ has higher precedence over *
=> Content at incremented address
*(p++) => Same as above
(*p)++ => Value is retrieved and incremented; e.g. here it will be (11)++ = 12
- Same concept applies to pre-increment, post-decrement, and pre-decrement as well.
Pointer Expression
- p + n => p + (n * size of data_type pointer by p). i.e. if p = 1000 and
= 1000 + 2 * 1 = 1002 if p points to char
Relationship between Pointers & Arrays
1. Using pointers in manipulating 1-d array
x[n] = *(p+n)
= *(x+n) i.e. x[3] = *(1000+3) = *(1000+3*2) = *(1006) = 33
2. Use of pointers in manipulating 2-d arrays
int x[3][4] = { // x[#rows][#cols]
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
int *p = x;
x[i][j] = *(p+i * #cols +j); // #cols = no. of elements in a row
e.g. x[2][3] = *(p+2*4+3)
= *(p+8+3) = *(p+11)
= *(1000+11*2) = *(1022)
= content at address 1022
= 4th element in 3rd row = 12 i.e. array index begins at 0
Pointers and Strings
- string <- array of characters
- char tv[20 = "ONIDA";
tv[0] = '0'......tv[4]='A'........tv[5]='\0'
- if string length is 'n' then
- 'n+1' location should contain null character as string terminator
Finding length of a string and printing it in reverse order
int n,i;
char tv[20]="ONIDA";
char *p = tv;
char *q;
q = p;
while(*q!='\0')
q ++;
n = q - p;
printf("Length of %d\n",n);
--p; /* to make p point to 'A' as it now points to '\0' */
while(n!=0)
{
putchar(*p);
p --;
n --;
}
More On Strings
1. char tv[20] = "ONIDA";
2. char tv[] = "ONIDA";
3. char *tv = "ONIDA";
1 => Compiler will allocate 20 bytes of memory and will store "ONIDA" in first 5 bytes and '\0' in 6th byte. Remaining bytes will empty
2 => Compiler will allocate just enough memory to store "ONIDA".
=> Here, tv is constant pointer so tv++ or tv+5 are invalid
3 => Compiler will allocate enough bytes automatically counting number of characters in string with an extra byte for null character.
=> Here, tv is a pointer variable so tv++ or tv+10 are all valid.
Initializing a 2-d char array
- char city[3][]={"Chennai","Calcutta","Delhi"};
- Howver,
As row size can be omitted which is nothing but no. of elements in array. But column size cannot be omitted.
Source: Data Structures Using C by Balakrishnan & Srinivasan, BPB Pub.
To be contd...