140x Filetype PDF File size 0.46 MB Source: gacbe.ac.in
UNIT – V CHAPTER XI POINTERS 11.1 INTRODUCTION • A pointer is a derived data type in c. (iv) Pointers are helpful in traversing through • Pointers contains memory addresses as arrays and character strings. The strings their values. are also arrays of characters terminated by the null character (‘\0’). • A pointer is a variable whose value is the (v) Pointers also act as references to different address of another variable, i.e., direct types of objects such as variables, arrays, address of the memory location. functions, structures, etc. In C, we use • Like any variable or constant, you must pointer as a reference. declare a pointer before using it to store (vi) Storage of strings through pointers saves any variable address. memory space. • Pointers can be used to access and (vii) Pointers may be used to pass on arrays, manipulate data stored in the memory. strings, functions, and variables as Advantages arguments of a function. (i) Pointers make the programs simple and (viii) Passing on arrays by pointers saves lot of reduce their length. memory because we are passing on only (ii) Pointers are helpful in allocation and de- the address of array instead of all the allocation of memory during the execution elements of an array, which would mean of the program. passing on copies of all the elements and Thus, pointers are the instruments thus taking lot of memory space. dynamic memory management. (ix) Pointers are used to construct different (iii) Pointers enhance the execution speed of a data structures such as linked lists, queues, program. stacks, etc. 11.2 ACCESSING THE ADDRESS VARIABLE • The operator & immediately preceding a #includevariable returns the address of the variable #include associated with it. void main() Example { p=&quantity; int x=125; • Would assign 5000 (the location of float p=20.345; quantity) to the variable p. char a=‘a’; • The & operator is a address operator. clrscr(); • The & operator can be used only with a simple variable or an array element. printf(“%d is stored at addr %u\n”,x,&x); &125 pointing at constants printf(“%f is stored at addr %u\n”,p,&p); int x[10]; illegal printf(“%c is stored at addr %u\n”,a,&a); &x pointing at array names getch(); &(x+y) pointing at expressions } • If x is an array then expression such as &x[0] is valid. DECLARING POINTER VARIABLES INITIALIZATION OF POINTER Syntax VARIABLES data_type *pt_name; • The process of assigning the address of a 1. The * tells that the variable pt_name is a variable to a pointer variable is known as name of the pointer variable. initialization. 2. Pt_name needs a memory location. • All uninitialized pointers will have some unknown values that will be interpreted as 3. Pt_name points to a variable of type memory addresses. data_type. • They may not be valid addresses or they Example may point to some values that are wrong. int *p; • Once a pointer variable has been declared • Declares the variable p as a pointer we can use the assignment operator to variable that points to an integer data type. initialize the variable. • The declarations cause the compiler to Example alocate memory locations for the pointer 1. int q; 2. int q; 3. int x,*p=&x variable p. int *p; int *p=&q p=&q; Illegal statement int *p=&x, x; • We can also define a pointer variable with an initial value to NULL or 0. int *p=null: int *p=0;
no reviews yet
Please Login to review.