Tuesday, October 12, 2010

Software Engg:Introduction to Programming:Pointers in C Language

Lecture Number :5
Topic : Pointers in C Language

What is Pointer?
 A pointer provides a way of accessing a variable without referring to a variable directly. A pointer is a special variable in C, which holds an address as its value. The unary operator * (de-reference) is used in both creating and dereferencing a pointer. The unary operator * may also be called indirection; either term is correct. With pointers, it’s possible to change a variable’s value even when the variable is not in scope.

Create A Pointer

A pointer, which holds a memory address, is created with the dereference operator char c; for example, means that the value referenced by c is a character char* c; means that c holds an address whose value is a character. Thus, c is said to point to a character value indirectly. Upon creation, a pointer is said to be a NULL pointer, meaning it points to no object inside of our program. The esterick tell the compiler that these variables will contain addresses (not values) and the int tells it that the address will point to integer variables.

Giving Pointer A Value


Since a pointer always holds an address, the pointer can be given a value by using the address of operator & together with a variable of the same type as the pointer. A pointer MUST be given a value BEFORE you can dereference it!
char *pc;
char c = ‘y’;
pc = &c;
Dereferring A Pointer 


The dereference operator * is used to get the value stored at a memory location. To dereference a pointer, we simply prefix it with the dereference operator

char* pc;
char c=‘Y’, c2 = ‘N’;
pc = &c;
c2 = *pc;

  • // We can store this address in a variable
  •  // declaring a variable that points to address of some variable
  • // save address of x in a variable ptrX
  •  ptrX = &x;
  • // now you can access value of x by following means:
  •   printf( "%d", *ptrX );
  •  // you can always see the address as well 
  • printf( "%u", ptrX );
Value of Pointer

If we pass a variable to a function, a copy of the variable’s value is taken by the receiving parameter. If this value is a pointer, however, a copy of the address is passed. This address can then be dereferenced and the value of the address read and/or changed. This allows us to change a variable when it is not in scope.

Final Notes to remember

  •  Pointers ALWAYS hold an address.
  • To get the address, just use the pointer’s name (identifier)
  • To get the value stored at that address, use the dereference operator in front of the pointer’s name
Important Examples to Clear Concepts of Pointers
·         int x;     means x is an integer
·         if x is an integer then &x must be the address of an integer
·         int *px;  means the contents of px is an integer
·         if *px is an integer then px must be the address of an integer            
·         x           means give me the value of x (integer)
·         &x        means give me the address of x (address)
·         px          means give me the value of px (address)
·        *px         means give me the value of the contents of  px 
Example No 1
Example No 2
Example No 3
 Example No 4
 Example No 5
  Example No 6 
 



No comments:

Post a Comment