Monday, 18 October 2010

33. Pointer Expressions

Let us now see what are pointers and how they can be used in various expressions. If i=5 then expression &i returns the adderss of i. If we so desire, this address can be collected in avariable by saying,

j=&i;

But remember that j is not an ordinary variable like any other integer variable. It is a variable, which contains the address of another variable. Since j is a variable the compiler ust probide it space in memory. Once again, the following memory map would illustrate the contents of and j.

As you can see, i's value is 5 and j's value is i's address.

But wait, we can't use j in program without declaring it. And since j is a variable, which contains the address of i, it is declared as,

int *j;

This declaration tells the conpiler that j will be used to store the address of an integer value - in other words j points to an integer. How do we justify the usage of  * (pointer).

int *j;

Let us go by the meaning of *. It stand for 'value at address'. Thus, int *j would mean, the value at the address contained in j is an int.

Look at the following declarations,

int *alpha;
char *ch;
float *s;

Here, alpha, ch and s are declared as pointer variables, i.e. variables capable of holding addresses. Remember that, addresses are always going to be whole numbers, therefore pointers always contain whole numbers. The declaration float *s does not mean that s is going to contain a floating-point value. What it means is, s is going to contain the address of a floating-point value. Similarly, char *ch means that ch is going to contain the address of a char value.

Pointer we know is a variable, which contains address of another variable. Now this variable itself could be another pointer. Thus, we now have a pointer, which contains another pointer's address. The following example should make this point clear.

#include<stdio.h>
main( )
{
int i=5;
int *j;
int **k;

j=&i;
k=&j;
printf(" \n Address of i = %u",&i);
printf(" \n Address of i = %u",j);
printf(" \n Address of i = %u",*k);
printf(" \n Address of j = %u",&j);
printf(" \n Address of j = %u",k);
printf(" \n Address of k = %u",&k);

printf("\n\n Value of j = %u",j);
printf("\n Value of k = %u",k);
printf("\n Value of i = %d",i);
printf("\n Value of i = %d",*(&i));
printf("\n Value of i = %d",*j);
printf("\n Value of i = %d",**k);
getch();
}

The output of the above program would be:

Address of i = 6589
Address of i = 6589
Address of i = 6589
Address of j = 3275
Address of j = 3275
Address of k = 7234

Value of j = 6589
Value of k = 3275
Value of i = 5
Value of i = 5
Value of i = 5
Value of i = 5

The following memory map would help you in tracing out how the program prints the above output.
Observe how the variables i, j and k have been declared,

int i;
int *j;
int **k;

Here, i is an ordinary int, j is a pointer to an int, whereas k is a pointer to a pointer. In principle, there could be a pointer to a pointer to a pointer, or a pointer to a pointer to a pointer/ There is no limit on how far can we go on extending this definition.

Taken from 'Understanding Pointers In C'
post by Arnob

32.The & and * Operators in POINTER

Consider the declaration,

int i=5;

This declaration tells the C compier to

  • Reserve space in memory to hold the integer value.
  • Associate the name i with this memory location.
  • Store the value 3 at this location.
We may represent i's location in the memory by the following memory map:

we see that the computer has selected memory location 6589 as the place to store the value 5. This location number 6589 is not a number to be relied upon, becuse some othertime the computer may choose a different location for storring the value 5. The important point is, i's address in memory is a number.

We can print this adderss through the following program:

#include<stdio.h>
main( )
{
int i=5;
printf("\n Address of i=%u", &i);
printf("\n Value of i=%u", i);
getch();
}

The output of the above program would be:

Address of i=6589
Value of i=5

Now look at the first printf( ) statement carefully. The '&' operator used in this statement is C's 'address of' operator. The expression &i returns the address of the variable i, which in this case happens to be 6589.

The other pointer operator available in Cis '*', called 'value at address' operator. It returns the value stored at a porticular address. The 'value at address' operator is also called an 'indirection' operator.

post by Arnob.
taken from 'Understanding pointers In C' 

Sunday, 17 October 2010

31. Introduction to Pointers

Introduction to Pointers
 Pointer is new type of variable that holds the memory address of another variable.
If a variable p contains the address of another variable q, then p is said to point to q.

To declare a pointer variable: add a asterisk to the type you want to point to.
int *a;
Declares a variable a of type int *, which can be used to hold the address of (or a “pointer to”) an integer.

Two unary operators (“inverses”):
& operator - “address of” operator.
Returns the address of the variable it precedes.
int *p = &q;
* operator - “dereference” or “value of” operator.
Accesses the variable that the pointer points to.
Returns the value stored at the address that the pointer points to.
int r = *p;
Pointers: Example 1


void main() {
int x = 1, y = 2, z = 3;
int *ip;
ip = &x;
z = *ip;
printf("%d", z); // prints value to x
printf("%d", *ip); // prints value to x
}




The * operator dereferences the pointer to get at the variable we’re pointing to.
In short: don’t confuse the * (dereference/value of) operator with the * in the declaration of a pointer variable (or with multiplication)!
int *p; // pointer declaration
*p = 100; // dereferencing

Another Example

int x = 1, y = 2;
int *ip;
char c;
char *cp;
ip = &x; /* ip now points to x */
printf( "%d\n", *ip ); /* prints 1 */
printf( "%d\n", *ip + 2 );
/* prints 3 */

y = *ip; /* y is now 1 */
*ip = 0; /* x is now 0 */
printf( "%d\n", x ); /* prints 0 */
//cp= &x; /* doesn’t work; types don’t match */
/*cp is not pointing anywhere */
*cp = ’z’;

cp = &c;
*cp = 'z';
printf( "%c\n", c ); /* prints z */

Pointer Arithmetic

Pointer addition: pointer plus int
if a pointer p points to an element of an array, then p + i is a pointer (of the same type) pointing to the ith element after the element pointed to by p.
Pointer subtraction: pointer minus pointer
If p and q point to elements of the same array, then q - p gives the number of elements between p and q.

Pointer comparison: pointer relation pointer
Permissible relations: ==, !=, <, <=, >, >=
If p and q point to elements of the same array, then p < q is true if p points to an earlier member of the array than q does. Note: CAN’T add two pointers, or perform any sort of multiplication, etc. A pointer is a physical memory location, represented by an integer, but you should never think of them as integers. (Try it!). Pointer arithmetic works at the level of “the next element in the array”, NOT at “the next physical memory address”.
Don’t Get Confused!

 Post by j.siam,
 Ref-: Md Munirul Haque