Saturday, 27 July 2013

Pointers in c

 A pointer is an address. Instead of  a variable, it is a pointer to a variable which is  stored somewhere in the memory of the program. Let  examine the a program [POINTER.C] which has some pointers to understand the concept of pointers.
C Programming
C Programming


#include "stdio.h"
/* illustration of pointer use */
void main( )
{
int i, *p1, *p2;
i = 39;        /* any  value */
p1 = &index;   /* the address of i */
p2 = pt1;
printf("The value is %d %d %d\n", i, *p1, *p2);

*p1 = 13; /* lets changes the value of i*/
printf("The value is %d %d %d\n", i, *p1, *p2);
}

We declare the pointers with the help of  star(asterisk).
Lets examine the first statement, it is clear that we assign 39 to the variable i. 
In next  statement, we assign the address of i to pt1."&" shows th address.
In this example, p1 and p2 are pointers, and the variable i is a simple variable.Before explaining the rest of program lets have look on Two very important s rule for using the pointers.



Two very important rules:

1. A variable have an ampersand (&) in front of it      refers to the address of the variable. line no 7. 
2.  A pointer with a star in front of it define  the value  of the variable.

 pointed to by the pointer. line no.10.

Memory aids::

1. Think of & as an address.

2. Think of * as a star referring to stored.

 As a pointers p1 and p2 do not contain a  value but an address of a variable. Line 7 in program assigns the pointer p1 to point to the variable i.


Line 10 modifies the value by using the pointer. because p1 points to the variable index, then putting a star in front of the pointer refers to the memory location to which it is pointing
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

Related Posts:

  • C language 'C' is a well known programming language which was developed by Dennis  Ritchie  in 1972 in  AT&T Bell Labs. This programming language was influenced by B , BCPL ,  ALGOL, and some other programming… Read More
  • Scope Rules In C A scope in any program is a that region of the program where a defined variable can have its existence and beyond that variable can't be accessed. The three places where variables can be declared in C programming l… Read More
  • Operators in C An operator is a symbol that instruct  the compiler to perform specific mathematical or logical operation. C language  has following type of operators: C Programming •  Arithmetic Operators •  … Read More
  • Decision Making Structure In C Decision making structures allows the programmer to specify one or more conditions to be evaluated or tested by the compiler or , along with a statement or set of statements to be executed if the condition is  … Read More
  • Storage Classes in c A storage class in C defines the scope (visibility) and life-time of a variables and/or a functions .These specifiers precede the type that they modify. There are some storage classes, which are  used in a C Pr… Read More

0 comments:

Post a Comment