This article explains about Accessing The Address of a variable, Declaring and Initializing Pointers, Accessing a Variable Through Pointer, Pointer Expressions, Pointer Comparison, Pointers and Arrays, Pointers and Char Strings, Pointers and Functions, Pointers and Structure.
- To access variable outside function
- More efficient way to handle data tables
- Length and complexity can be reduced
- Execution speed increased
- Pointer array to char strings results in saving of data storage
- Can though, cause problems
Understanding Pointers
- Computer memory is a sequential collection of storage cells for data and instructions
- Each cell, has an address associated with it
- The addresses are numbered contiguously
- The last address depends on memory size
- During execution system always associates name of a variable with it’s address
Accessing The Address of a variable
- The actual location of a variable in the memory is system dependent
- It may vary throughout the execution
- Address operator is used to access it
- &125, int x[10], &x, &(x+y) are illegal use of address operator
- &x[0] or &[x+i] are valid operations
Declaring and Initializing Pointers
- int *p; // p contains address of an int
- float *x // x contains address of a float
- p = &quantity // quantity is an int variable
- p = &f where f is float is an erroneous statement, because *p results in a wrong ans
- int x, *p = &x; // initializes p to address of x
- int *p=&x, x is invalid
Accessing a Variable Through Pointer
- The indirection operator (*) is used to access the value of a variable by its ptr
- * can be remembered as value at address
- int n = *p // int *p = &quantity is done
- int n = *&quantity // is = quantity
- *5445 where 5445 is a valid location does not yield the content at that address
- *x=25 changes value of f indirectly
Pointer Expressions
- y = *p * *q // (*p) * (*q)
- sum += *p , *p2 = *p2 + 10
- z = 5 * - *p2/ *p1 //(5 * (-(*p2)))/(*p1)
- in above /* is not acceptable
- p+1, p – 4, p++, p- - are allowed
- p2 –p1 for the same array are no of elements in between them
Pointer Comparison
- p1 > p2 is true if p1 is having higher memory location than p2
- p1 == p2 and p1 !=p2 are also accepted as valid
- pointers to related variables like array or string elements makes sense
- Pointers can not be used in division, multiplication or addition
Pointers and Arrays
- Array name is a pointer to itself
- ANSI standard makes &a = a
- int x[10], *p = x; makes p = &x[0]
- while ( p < &x[4]) { sum+=*p; p++ }
- The compiler allocates contiguous space for all the elements row-wise in multidimensional arrays
- int x[10][20] and int *p = x is allowed
- When we increment i by 1, p will be incremented by the size of the row and not the size of the element itself
- p is pointer to first row, p+i is pointer to ith row, *(p+i) is the first element of the ith row
- *(p+i) + j is jth element of ith row
- *(*(p+i)+j) value stored in cell (i,j)
Pointers and Char Strings
- char name[20], *cptr = name; is valid
- while (*cptr != ‘\0’) or (*cptr) is true until the end of the string is reached
- while (*cptr); length = cptr – name;
- char *name; name = “Delhi” is accepted
- Char arrays with variable row length are called ragged arrays, better handled by ptrs
Pointers and Functions
- Passing a pointer to a function is known as call by reference and function works on actual variables in that case
- void copy(char *s1, char*s2) { while ((*s1++ = *s2++) != ‘\0’ }
- Pointer parameters are very common in string functions like above
- *s++ and *++s are different!
- type (*ptr)() is a pointer to a function with return type as type
- It is different than type *ptr()
- double (*p1)(), mul();
- p1 = mul;
- (*p1) (x,y) is same as mul(x,y)
- It is used in system programming
Pointers and Structures
- struct inventory { char name[30];
- int number;
- float price; }
- product[2],*p;
- p = product; // assign zeroth element
- p -> name is same as (*p).name
- ++ptr->count increments count
- (++ptr)->count increments ptr and then return count
- ptr++->count is legal and increments ptr after accessing count
- *ptr->p fetches whatever p points to
- *ptr->p++ increments p after accessing whatever it points to
- (*ptr->p)++ increments whatever p points to
- *ptr++->p increments ptr after accessing whatever it points to
Points on Pointers
- A pointer containing garbage can not be detected in certain cases
- Abundance of operators are to be used carefully with pointers
- Passing pointers to double dimension arrays need other than first size to be specified
- Pointer to auto array of a function can not be passed back