Вы находитесь на странице: 1из 3

C-Programming

Pointers

POINTERS
Pointer Pointer is a variable, which stores the address of another variable. Pointer cannot be initialized to value like ptr=4; etc. A pointer is a variable that holds a memory address of another variable. This address is usually the location of another variable in memory. If one variable contains the address of another variable, the first variable is said to point to the second. If a variable is going to hold a pointer, it must be declared as such. A pointer declaration consists of a base type and an * (asterisk), and the variable name. The general form of declaring a pointer variable is type *ptr; where type is any valid C type and ptr is the name of the pointer variables. For e.g. char *a; declares a pointer to a character. Consider the declaration and initialization of the following variable a : int a; a = 3; Now in memory it occupies a place having some address. Memory representation of a can be represented as : a Location name Location Value Location number or address

3
65524

Declaration of pointer : int *ptr; Intialization of pointer : ptr = &a; Hence to declare a pointer we use * (asterisk) sign in front of variable name. For initialization of pointer we use & sign. It represents the address of pointer. Here * is known as value at address operator and & is known as address of operator.

Teacher : Sandeep Sangwan (9417565720)

C-Programming

Pointers

Memory representation of pointer variable :

65522 1
65520

cp

Location name Location Value Location number or address

Pointers are used frequently in C, as they offer a number of benefits to the programmers. They include : 1. Pointer are more efficient in handling arrays and data tables. 2. Pointers can be used to return multiple values from a function via function arguments. 3. Pointers permits references to functions and thereby facilitating passing of functions as arguments to other functions. 4. Pointers allow C to support dynamic memory management. 5. Pointers provide an efficient tool for manipulating dynamic data structures such as structures, linked lists, queues, stacks and trees. 6. Pointers reduce length and complexity of programs. 7. They increase the execution speed and thus reduce the program execution time. POINTER ARITHMETIC : As a pointer holds the memory address of a variable, some arithmetic operations can be performed with pointers. C supports four arithmetic operators that can be used with pointers, such as Addition + Subtraction Incrementation ++ Decrementation Pointers are variables. They are not integers, but they can be displayed as unsigned integers. The conversion specifier for a pointer is added or subtracted. For example, ptr ++ causes the pointer to be incremented, but not by 1. ptr causes the pointer to be decremented, but not by 1. The following program segment illustrates the pointer arithmetic : The integer variable a would occupy bytes 2000 and 2001. int a, *ptr; a = 120; ptr = &a; ptr++; printf(%lu,ptr); The above C program segment displays 2002. The pointer ptr is originally assigned the value 2000. The incrementation, ptr++, increments the number of bytes of the storage class for the particular machine. If the

Teacher : Sandeep Sangwan (9417565720)

C-Programming

Pointers

system used four bytes to store an integer, then ptr++ would have resulted in ptr being equal to 2004. The general rule for pointer arithmetic is that pointer performs the operation in bytes of the appropriate storage class. # Pointer-to-Pointer : Pointer-to-Pointer is variable which stores the address of another pointer.

Declaration :
int **cp;

Initialization :
cp=&ptr;

Memory representation of Pointer-to-Pointer variable :

65522 1
65520

cp

Location name Location Value Location number or address

Note: In above examples, To print the value of a i.e. 3, we can use the following statements printf(%d,a); printf(%d,*ptr); printf(%d,**cp); To print the address of a we can use the following statements printf(%ld,&a); printf(%ld,ptr); printf(%ld,*cp); To print the address of ptr we can use the following statements printf(%ld,&ptr); printf(%ld,cp);

Teacher : Sandeep Sangwan (9417565720)

Вам также может понравиться