Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Pointers in C Programming, Study Guides, Projects, Research of Computer Programming

The concept of pointers in C programming. It defines pointers as memory variables that store memory addresses and explains how they are declared and used. It also lists the benefits of using pointers and how they can be used to handle arrays, return multiple values from a function, and manipulate dynamic data structures. The document also covers pointer arithmetic operations such as incrementing, decrementing, adding and subtracting numbers from pointers. It also explains how pointers can be used with arrays and how they can be passed as function arguments.

Typology: Study Guides, Projects, Research

2021/2022

Available from 01/02/2024

dheeraj-kumar-karne
dheeraj-kumar-karne 🇮🇳

6 documents

1 / 14

Toggle sidebar

Related documents


Partial preview of the text

Download Pointers in C Programming and more Study Guides, Projects, Research Computer Programming in PDF only on Docsity! UNIT-V POINTERS Definition: A pointer is a memory variable that stores a memory address. Pointer can have any name that is legal for other variable and it is declared in the same fashion like other variables but it is always by ‘*’ operator. A pointer is a derived data type in ‘C’. Benefits of Pointers (or) uses: 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 arguments. 3) Pointers permit references to function and thereby facilitating passing of functions as arguments to other functions. 4) The use of pointer arrays to character strings results in saving of data storage space in memory. 5) Pointers allow ‘C’ to support dynamic memory management. 6) Pointers provide an efficient tool for manipulating dynamic data structures such as structures, linked lists, queues, stacks and trees. 7) Pointers reduce length and complexity of programs. 8) They increase the execution speed and thus reduce the program execution time. Understanding Pointers: Computers memory is a sequential collection of storage cells, each cell commonly known as a byte, has a number called address associated with it. The starting address from zero to last address depends on the memory size. For Ex: 64k memory 0 1 . . . . . 65,535 Memory cell address Whenever we declare a variable, the system allocates somewhere in the memory, an appropriate location to hold the value of the variable. Ex: int quantity=179; Representation of variable: quantity -> variable 179->value 5000->address During execution of program, the system always associates the name quantity with the address 5000. We may have access to the value 179 by using the either quantity or 5000. Since memory addresses are simply numbers, they can be assigned to some variables that can be stored in memory like any other variable such variables that hold memory addresses are called pointer variables. A pointer variable is nothing but a variable that contains an address, which is a location of another variable in memory. Since a pointer is a variable, its value is also stored in the memory in another location. Suppose, we assign the address of quantity to a variable p. Pointer variable: Variable Value Address quantity 5000 P 5048 Since the value of the variable p is the address of the variable quantity. We may access the value of quantity by using the value of p. we say that the variable p ‘points’ to the variable quantity.  Memory addresses within a computer are referred to as pointer constants.  We cannot save the value of a memory address directly. We can only obtain the value through the variable stored there using the address operator (&). The value thus obtained is known as pointer value. Accessing the Address of a variable: The actual location of a variable in the memory is system dependent and the address of a variable is not known to us immediately. How can we then determine the address of a variable? This can be done with the help of the operator & a variable in C. The operator ‘&’ immediately preceding a variable returns the address of the associated with it. Ex: P=&quantity; Assign address 5000 to the variable p. The &operator can be used only with a simple variable or an array element. 179 5000 Here p1 is a pointer variable which stores the address of ‘a’, this is also stored in another address 6000. The variable p2 stores the address of a pointer p1, thus p2 is known aspointer to pointer. Ex: #include<stdio.h> #include<conio.h> void main() { int a=5, *p1, **p2; p1=&a; p2=&p1; printf(“Address of a =%u”,p1); printf(“address of p1 =%u”,p2); printf(“Address of a =%d”,**p2); a 65524 printf(“Value of p2 =%u”,p2); printf(“Address of p1 =%u”,p2); p1 65522 printf(“Value of p1 =%u”,p1); printf(“Address of a =%u”,p1); p2 a printf(“Value of a =%d”,**p2); getch(); } Pointers as function arguments: When we pass addresses to a function, the parameters receiving the addresses should be pointers. The process of calling a function, the parameters to pass the addresses of variable is known as call by reference. Ex: #include<stdio.h> #include<conio.h> void exchange(int *,int *); main() { int x,y; x=100; y=200; clrscr(); printf(“Before exchange x=%d, y=%d”,x,y); exchange(&x,&y); 5 65524 65522 printf(“After exchange x=%d, y=%d”,x,y); getch(); } void exchange(int *a,int *b) { int t; t=*a; *a=*b; *b=t; } Functions Returning Pointers: A function can return a single value by its name or return multiple values through pointer parameter. Since pointer are a data type in c. Passing by Address: Address Arithmetic (or) Pointer Arithmetic: Pointer is a data type in c, certain operations are allowed on it. These operations are call pointer arithmetic or address arithmetic. We can perform arithmetic operations on pointer variable just as you can a numeric value. As we know that, a pointer in C is a variable which is used to store the memory address which is a numeric value. The arithmetic operations on pointer variable effects the memory address pointed by pointer. Valid Pointer Arithmetic Operations  Adding a number to pointer.  Subtracting a number form a pointer.  Incrementing a pointer.  Decrementing a pointer.  Subtracting two pointers.  Comparison on two pointers. Invalid Pointer Arithmetic Operations  Addition of two pointers.  Division of two pointers.  Multiplication of two pointers. Incrementing a Pointer Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable is 32-bit(4 bytes). Now, when we increment pointer ptr ptr++; it will point to memory location 5004 because it will jump to the next integer location which is 4 bytes next to the current location. Incrementing a pointer is not same as incrementing an integer value. On incrementing, a pointer will point to the memory location after skipping N bytes, where N is the size of the data type(in this case it is 4). ptr++ is equivalent to ptr + (sizeof(pointer_data_type)). "Incrementing a pointer increases its value by the number of bytes of its data type" A character(1 bytes) pointer on increment jumps 1 bytes. An integer(4 bytes) pointer on increment jumps 4 bytes. This is a very important feature of pointer arithmetic operations which we will use in array traversal using pointers. Decrementing a Pointer Similarly, Decrementing a pointer will decrease its value by the number of bytes of its data type. Hence, after ptr--; ptr will point to 4996. ptr--; is equivalent to ptr - (sizeof(pointer_data_type)). Adding Numbers to Pointers Adding a number N to a pointer leads the pointer to a new location after skipping N times size of data type. ptr + N = ptr + (N * sizeof(pointer_data_ype)) For example, Let ptr be a 4-byte integer pointer, initially pointing to location 5000. Then ptr + 5 = 5000 + 4*5 = 5020. Pointer ptr will now point at memory address 5020. Subtracting Numbers from Pointers Subtracting a number N from a pointers is similar to adding a number except in Subtraction the new location will be before current location by N times size of data type. printf(“Addresses are %u, %u, %u”,p1,p2,p3); printf(“After incrementation\n”); p1++; p2++; p3++; printf(“Addresses are %u%u%u”,p1,p2,p3); getch(); } Addresses are 65524 65520 65517 After incrementation 65526 65524 65518 Pointers and Arrays: When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the address of the array in contiguous memory locations. The base address is the location of first element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. Ex: int x[5]={1,2,3,4,5}; Suppose the base address of x is 1000 and assuming that each integer requires 2 bytes, the five elements will be stored as follows. x[0] x[1] x[2] x[3] x[4] ----element value 1000 1002 1004 1006 1008 ------address Base address The name x is defined as a constant pointer pointing to the first element, x[0] and therefore the value of x is 1000, the location where x[0] is stored. x=&x[0]=1000 If we declare p as an integer pointer, then we can make the pointer p to point to the array x by the following assignment. p=x; equal to p=&x[0]; 1 2 3 4 5 Now we can access every value of x using p++ to move from element to another. p=&x[0]=1000 p+1=&x[1]=1002 p+2=&x[2]=1004 p+3=&x[3]=1006 p+4=&x[4]=1008 The address of an element is calculated using its index and the scale factor of the data type. address of x[3]=base address+(3*scale factor of int) =1000+(3*2)=1006 Whenever pointer to an array is declared, the pointer variable has to be initialized to the base address of the array. Ex: int a[5], *pt; pt=&a[0]; Ex: #include<stdio.h> #include<conio.h> void main() { int a[10],*pt,i,n; pt=&a[10]; (or) pt=a; printf(“Enter the size of the array”); scanf(“%d”,&n) printf(“Enter the elements”); for(i=0;i<n;i++) scanf(“%d”,pt+i); printf(“The elements are”); for(i=0;i<n;i++) printf(“%d”,(pt+i)); getch(); } Pointers to 2D Arrays: A pointer to a 2D array can be declared similar to a pointer to 1D array. Ex: int a[2][3]={1,2,3,4,5,6}; a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] 1 2 3 4 5 6 5000 5002 5004 5006 5008 5010 Initialization is pt=&a[0][0]; The address of an element can be calculated by address = base address + column size * i + j i=row position j=column position address of a[1][1] = 5000+ (3*1+1)*2 = 5000+(3+1)*2 = 5000+8 = 5008 Arrays of Pointers: Homogeneous collection of pointers is referred to array of pointers, i.e. collection of pointers of the same data type. The pointers are declared in array form like other data type. An integer pointer array mark of size can be declared as. Syntax:datatype *variable[size]; Ex: int *marks[20]; A pointer may also be used for more than one array declaration of same data type. int s1[20],s2[20],s3[20]; marks[0]=&s1[0]; marks[1]=&s2[0]; marks[2]=&s3[0]; Pointer to functions: #include<stdio.h> #include<conio.h> int *larger(int *,int *); main() { int a=10; int b=20; int *p; p=larger(&a,&b); printf(“%d”,*p); } int *larger(int *x,int *y) { if(*x>*y) #include<stdio.h> #include<conio.h> int large(int *,int *); void main() { int x,y,z; printf(“enter 2 values”); scanf(“%d%d”,&x,&y); z=large(&x,&y); printf(“large=%d”,z); } int large(int *p,int *q) { if(*p>*q)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved