
Computer Programming Unit –3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it’s very necessary to learn pointers and to become a perfect c programmer. As you know, every variable is a memory location and every memory location has address which is defined and accessed using ampersand (&) operator. #include<stdio.h> #include<conio.h> void main( ) { int var1; int var2[10]; printf(“Address of : %x”, &var1); printf(“Address of : %x”, &var2); getch(); } Pointers: pointer is a variable that stores address of another variable. Pointer are used to allocate memory dynamically i.e at runtime in c-language. Pointer variable might belonging to any of the data type such as int, float, char, double, short, e.t.c. Syntax : data_type * variable_name; Example : int *p; char * ch; Where * is used to denote the pointers. P is the pointer variable and not a normal variable. Key points to remember about pointer in c Normal variable stores the value whereas pointer variable stores the address of the variable. The content of the C pointer always be a whole number i.e. address. Always C pointer is initialized to null, i.e. int *p = null. The value of null pointer is 0. & symbol is used to get the address of the variable. * symbol is used to get the value of the variable that the pointer is pointing to. 1 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 If a pointer in C is assigned to NULL, it means it is pointing to nothing. Two pointers can be subtracted to know how many elements are available between these two pointers. But, Pointer addition, multiplication, division are not allowed. The size of any pointer is 2 byte. Pointer is more efficient in handling array and structure. It reduces the length of the program and execution time. 10 Pointers are one of the most distinct and exciting features of c-language. It provides power and flexibility to the language. DECLARING A POINTER A variable is a declared as a pointer that holds a memory address. Syntax for pointer declaration Datatype *pointer_name; Explanation: Data type: type of variable that pointer points to. Or data type whose value is stored in pointer name. Asterisk: it is called as indirection operator. It is also called as value at address operator. Pointer_Name: Must follow be all rules of variable. It Must be any c-identifier Ways of Declaring Pointer Variable: * can appears anywhere between Pointer_name and Data Type. int *p; or int* p; or int * p; 2 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 Example of Declaring Integer Pointer : int n = 20; int *ptr; Example of Declaring Character Pointer : char ch = 'A'; char *cptr; Example of Declaring Float Pointer : float fvar = 3.14; float *fptr; Example program for pointers in C: Initialization of Pointers or initializing pointers Pointer initialization is the process of assigning address of the variable to pointer variable. Pointer variable contains address of variable of same data type. In c-language address operator ‘&’ is used to determine the address of variable i.e it is used to get the address of variable. int a=10; int *ptr; // pointer declaration ptr = &a; // pointer initialization 3 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 int *ptr = &a; // initialization & declaration %p is a control specifier, which is used for displaying the address in hex format. int *p1; double *p2; char *p3; float *p4; Note : if you need a pointer to store the address of integer variable, then the data type of pointer should be int. By using *pointer we can access the value of variable example : double a=10; double *p; p=&a; Accessing variable through pointer: We can access the value of pointer using the de-reference operator and address of variable can be printed using address operator. Program : accessing value and address of Pointer #include<stdio.h> main() { int i = 3, *j, **k; j = &i; k = &j; printf("\nAddress of i = %u", &i); printf("\nAddress of i = %u", j); printf("\nAddress of j = %u", &j); printf("\nAddress of j = %u", k); } Output : Address of i = 65524 Address of i = 65524 Address of j = 65522 Address of j = 65522 4 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 POINTER OPERATORS C Pointers – Operators that are used with Pointers The operators & and * that are used with Pointers in C. The & operator is also known as “Address of” Operator. Pointer Operator in C Program : #include <stdio.h> int main(){ int num = 10; //Variable Initialization. int *p; //Pointer declaration. p = &num; //Assigning address of num to the pointer p printf("Address of variable num is: %p", p); return 0; } In order to create pointer to a variable we use “*” operator and to find the address of variable we use “&” operator. [box]Don’t Consider “&” and “*” operator as Logical AND and Multiplication Operator in Case of Pointer. Important Notes : 1. ‘&’ operator is called as address Operator. 2. ‘*’ is called as ‘Value at address’ Operator. 3. ‘Value at address’ Operator gives ‘Value stored at Particular address. 4. ‘Value at address’ is also called as ‘Indirection Operator’. 5 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 Pointer Operators : Live Program #include<stdio.h> int main() { int n = 20; printf("\nThe address of n is %u",&n); printf("\nThe Value of n is %d",n); printf("\nThe Value of n is %d",*(&n)); } Output: The address of n is 1002 The Value of n is 20 The Value of n is 20 6 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 Important point to note is: The data type of pointer and the variable must match, an int pointer can hold the address of int variable, and similarly a pointer declared with float data type can hold the address of a float variable. The * Operator is also known as Value at address operator. If you need a pointer to store the address of integer variable then the data type of the pointer should be int. int *p1; /*Pointer to an integer variable*/ double *p2; /*Pointer to a variable of data type double*/ char *p3; /*Pointer to a character variable*/ float *p4; /*pointer to a float variable*/ double a = 10; double *p; p = &a; Note: *p would give us the value of the variable a. The following statement would display 10 as output. printf("%d", *p); Similarly if we assign a value to *pointer like this: *p = 200; It would change the value of variable a. The statement above will change the value of a from 10 to 200. 7 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 8 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 Accessing a variable to pointer Null Pointer 9 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 PASSING POINTER TO A FUNCTION IN C PROGRAMMING (OR) PASSING POINTER AS FUNCTION PARAMETERS When we pass address to a function, the parameters receiving the address should be pointer. Just like any other argument, pointers can also be passed to a function as an argument. Let’s take an example to understand how this is done. When pointer is passed to function as an argument, address of memory location is passed instead of value. This is because, pointers stores the location of the memory and not the value. This method is known as call by reference or call by address in c. 10 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 ACCESSING AN ARRAY USING POINTER: This program declares the array of five element and the elements of that array are accessed using pointer. As we know that array is collection of data items that belongings to same data type in c. By using pointers that can be declared and accessed very easily. When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. void main { int *p,sum,i; int x[5]={5,9,6,3,7}; i=0; p=x; sum=0; printf(“Element \t Value \t Address \n\n”); while(i<5) { printf(“x[%d] %d %u \n”, i, *p, p); sum=sum+*p; i++; p++; } Printf(“\n sum=%d\n”, sum); Printf(“\n &x[0]=%u\n”, &x[0]); Printf(“\n p= %u\n”, p); } ACCESSING STRING MEMBERS USING A POINTER A string is array of characters, terminated with a null characters. Similar to arrays, we can use a pointers to access the individual characters in a string. void main() { char *name; int length; char *cptr = name; Name = “DELHI”; while(*cptr!=‘\0’) { Printf(“%c is stored at address %u\n”,*cptr,cptr); cptr++; } Length=cptr-name; Printf(“length of the string = %d\n”, Length); getch(); } 11 Prepared by Gajjala.varaprasad , JNTU Anantapur Computer Programming Unit –3 POINTER EXPRESSION Arithmetic operations between two or more pointers are not possible. But pointers can be used to perform arithmetic operations on the value they point to Pointer expressions: pointer variables can be used in expressions like variables. For example if p1 and p2 are properly declared and initialized pointers, then these statements are valid. e.g: a = *p1 * *p2 / *p3; b = 10* - *p3 / *p2; Pointer increment is valid in c. Ex: P++ or p=p1+2 are valid statements. When pointer is incremented, it increases it value by length of the data type. It points to memory location or data type. (1) Characters – 1 byte (2) integer – 2 bytes (3) Float – 4 bytes (4) double – 8 bytes like other variables pointer variables can be used in expressions If p1 and p2 are properly declared and initialized pointers, then the following statements are valid: Y=*p1**p2; Sum=sum+*p1; Z=5*-*p2/ *p1; *p2=*p2+10; *p1=*p1+*p2; *p1=*p2-*p1; NOTE: In the third statement there is a blank space between ‘/’ and * because the symbol /*is considered as beginning of the comment and therefore the statement fails.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages39 Page
-
File Size-