Document Details

Uploaded by Deleted User

Smt. R O Patel Women's College - Morbi

SMT R.O.PATEL WOMEN’S COLLEGE- MORBI

Tags

C programming pointers memory allocation computer science

Summary

This document provides an introduction to pointers in C programming, covering pointer declaration, address operators, arithmetic operations, and their advantages. It also describes call-by-value and call-by-reference function calls, dynamic memory allocation, arrays of pointers, and pointers to structures. The document is part of a course in computer science at SMT R.O.PATEL WOMEN’S COLLEGE- MORBI.

Full Transcript

SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C Unit-4 POINTER Introduction:  Pointer is a derived data type in C. It is built from the one of the fundamental data type available in C.  A...

SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C Unit-4 POINTER Introduction:  Pointer is a derived data type in C. It is built from the one of the fundamental data type available in C.  A pointer is a variable that points to or references a memory location in which data is stored.  Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location(address). It can be used for different purpose such as access the variable value at that address , to increment it to get address of next element and change the contents of this memory location via the pointer. Pointer declaration:  In C variable must be declared for its use. The poiner variable must be declared before it is used. A pointer is a variable that contains the memory location(address) of another variable.  The syntax is as shown below. type * variable_name ;  According to the above syntax type specifying the type of data stored in the location identified by the pointer. The asterisk(*) tells the compiler that variable_name is a pointer variable. variable_name is the name of the variable.  The unary operator * is called indirection or dereferencing operator, Ex, int *ptr; char *string; Address operator:  Once declare a pointer variable must be need to point it to something it can be done by assigning to the pointer the address of the variable.  The unary operator & called address of operator is used to assign the address of variable. Ex, int num=10; int *ptr; // Declaration ptr=&num; // Initialization 10 212 num ptr 1 Prepared By : Pandya Ruchita SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C  This places the address where num is stores into the variable ptr. If num is stored in memory 212 address then the variable ptr has the value 21260. void main() { int *ptr; int sum; sum=45; ptr=&sum; printf (“\n Sum is %d \n”, sum); printf (“\n The address of sum is = %u”, ptr); printf(“\n value of sum is = %d”,*ptr); } Advantage of using pointer 1. Pointers are more efficient in handling arrays and data tables 2. Pointers can be used to return multiple values from a function via function argument. 3. Pointers permit reference to functions and thereby facilitating passing of functions as arguments to other function. 4. The use of pointer arrays to character strings results in saving of data storage space in memory. 5. The main advantage of pointer is dynamic memory management. 6. Pointers provide an efficient tool for manipulating dynamic data structure such as linked list, queue, stack, trees. 7. Pointer reduce length and complexity of programs. 8. They increase the execution speed and thus reduce the program execution time. Important points to be remember while using pointer 1. Pointer must be initialized before it is used. 2. Pointer can be initialized while declaring. 3. Integers should point to elements of appropriate type. 4. when pointer is incremented , it is incremented as per the size of variable to which it points. 5. Two pointer are never added. 6. Pointer can be effectively used with array, function,string,structure etc. 7. Integer number can be added or subtracted from pointer. 8. & operator is used with pointer to get the address of variable which stores value 2 Prepared By : Pandya Ruchita SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C Pointer Arithmetic Pointer variables can also be used in arithmetic expression like adding and subtracting integer variable and manipulation from a pointer variable through the resulting expression. Different operations perform on pointer are as under: 1. Pointers can be incremented or decremented to point to different location. Ex, int x=20; int *ptr=&x; ptr++ ptr— here, ptr is a pointer to integer with address of variable x , if ptr has value 65526 then after the operation ptr++ (ptr=ptr+1) , the value of ptr would be 65528. Increment and decrement in pointer value is based upon the size of data type that it points to. 2.if ptr1 and ptr2 are properly declared and initialized pointers the following operations are valid Ex, ans=ans+ *ptr1; *ptr1 = *ptr2 +5; multi= *ptr1 * *ptr2; 3. When two pointers point to the same datatype , they can be compared using relational operators Ex, ptr1==ptr2 ptr1name,”Om”); p->salary=5000; The symbol-> is called arraow operator (member selection operator) is used to access the member of structure when accessed by pointer. Ex, #include #include struct emp { int no; char name; long int salary; }; void main() { //initialization struct emp *e; 9 Prepared By : Pandya Ruchita SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C clrscr(); printf("\n enter no : "); scanf("%d",&e->no); fflush(stdin); printf("\n Enter name : "); gets(e->name); printf("\n Enter salary : "); scanf("%ld",&e->salary); printf("\n No Name Salary"); printf("\n %d %s %ld",e->no,e->name,e->salary); getch(); } Call by value  In this kind of function, pass the value directly to the called function through the calling function. The called function does process and returns the value back to the calling function.  In this method the value of each of actual argument in calling function is copied into corresponding formal arguments of the called function.  Any change made to the arguments internally in the function are made only to the local copies of the arguments, It will not reflect to actual argument in calling function. Ex, #include < stdio.h> void swap(int a, int b); void main() { int a, b; a = 5; b = 7; printf("From main: a = %d, b = %d\n", a, b); swap(a, b); printf("Back in main: "); printf("a = %d, b = %d\n", a, b); } void swap(int a, int b) { int temp; 10 Prepared By : Pandya Ruchita SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C temp = a; a = b; b = temp; printf(" \n Inside function swap :"); printf(" a = %d, b = %d", a, b); } O/P From main: a = 5, b = 7 Inside function swap :a = 7, b = 5 Back in main: a = 5, b = 7 Call By Reference  In call by reference , not need to pass value directly to the called function , Instead of that pass the address of the variable which hold the value. It can be got by passing the address to the function.  By this method , change made to the parameters of function will affect the variables which called the function. #include < stdio.h> void swap(int *a, int *b); void main() { int a, b; a = 5; b = 7; printf("From main: a = %d, b = %d\n", a, b); swap(&a,&b); printf("Back in main: "); printf("a = %d, b = %d\n", a, b); } void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; printf(" Inside function swap "); printf("a = %d, b = %d\n", *a, *b); 11 Prepared By : Pandya Ruchita SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C } O/P From main: a = 5, b = 7 Inside function swap :a = 7, b = 5 Back in main: a = 7, b = 5 Dynamic memory allocation The process of allocation memory at run time is called dynamic memory allocation. Its advantages are: 1. When programmer actually need to use memory space at that time dynamically allocates so save memory space. 2. Programmer doesn’t need to know in advance how many variables are needed. There are mainly four types function available for dynamic memory allocation. malloc() It allocates request size of bytes and returns a pointer to the first byte of the allocated space. Syntax ptr-var=(cast type) malloc(byte size); Here, ptr-var is a pointer of type cast-type. It returns a pointer to an area of the memory with size byte-size. Ex, int *x; x=(int *)malloc(10*sizeof(int)); In above example , a memory space 100*size of int(200) bytes is reserved and the address of the first byte of the memory allocated is assigned to the pointer a of type of int. calloc() This function allocates space for an array of elements. This function is normally used for derived data type such as array and structure. malloc() allocates single block of storage space and calloc() allocates multiple block of storage. Syntax: ptr=(cast-type)calloc(n,element-size); Here, ptr is a pointer of type cast. The first argument n is number of variable for what need to allocate space. The second argument element-size is the size of each element in byte. 12 Prepared By : Pandya Ruchita SMT R.O.PATEL WOMEN’S COLLEGE- MORBI CS-02 Problem solving methodologies and programming in C If allocation of memory space is successful , all bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is no enough space NULL pointer is returned. Ex, struct emp { int no; char *name; float age; } ----------- ----------- struct emp *e; e=(struct emp *)calloc(10,sizeof(struct emp)); Here, array of 10 structure object is create. realloc() When need to change the size of previously allocated memory space then realloc() is used. It is known as reallocation of memory. Syntax: ptr=(cast-type)realloc(ptr,newsize); Here, ptr is the pointer to the original block of memory. The newsize specifies the size in byte , the function returns pointer to the first byte of new memory block.The newsize may be larger or smaller than the size. If the function is unsuccessful in locating additional space , it returns as a NULL pointer and original block is lost. Ex, ptr=(int *)malloc(20*sizeof(int)); ------- ------- ptr=(int *)realloc(30*sizeof(int)); free() The release of storage space becomes important when the storage is limited.free() function is used to release(deallocate) that block of memory which is created by malloc() and calloc(). Sytax: free(ptr); Here, the ptr is the pointer variable which previously allocated dynamic memory. Ex, ptr=(int *)malloc(20*sizeof(int)); free(ptr); 13 Prepared By : Pandya Ruchita

Use Quizgecko on...
Browser
Browser