








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 14
This page cannot be seen from the preview
Don't miss anything!
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:
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
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.
It is also possible to combine the declaration of data variable, the declaration of pointer variable and initialization of the pointer variable in one step.
int x,p=&x; /valid/ int p=&x,x; /not valid / We could also define a pointer variable with an value of NULL or 0. int p=NULL; int p=0; Accessing a variable through its pointer: ‘’ is known asindirection operator, dereferencing operator. int quantity,p,n; quantity =179; p=&quantity; n=p; Equivalent to n=&quantity; n=quantity; Pointers to Pointers: Pointers are available which contains address of another variable. Pointers variables also have an address. The pointer variable containing address of another pointer variable is calledpointer to pointer.
Syntax: datatype **variable; Ex : int a=10; int *p1=&a; int **p2=&p1; a 5000
p1 6000
p2 7000
Variable value address
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);
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.
ptr - N = ptr - (N * sizeof(pointer_data_ype))
For example, Let ptr be a 6-byte double pointer, initially pointing to location 5000. Then ptr - 3 = 5000 - 6*3 = 4982. Pointer ptr will now point at memory address 4982.
Subtracting Pointers The difference between two pointer returns indicates “How apart the two Pointers are. It gives the total number of elements between two pointers. For example, Let size of integer is 4 bytes. If an integer pointer 'ptr1' points at memory location 10000 and integer pointer 'ptr' points at memory location 10008, the result of ptr2 - ptr1 is 2.
/C program to show pointer arithmetic/** #include<stdio.h> #include<conio.h>
void main() { int int_var = 10, *int_ptr; char char_var = 'A', *char_ptr; float float_val = 4.65, *float_ptr;
/* Initialize pointers */ int_ptr = &int_var; char_ptr = &char_var; float_ptr = &float_val;
printf("Address of int_var = %u\n", int_ptr); printf("Address of char_var = %u\n", char_ptr); printf("Address of float_var = %u\n\n", float_ptr);
/* Incrementing pointers */ int_ptr++; char_ptr++; float_ptr++; printf("After increment address in int_ptr = %u\n", int_ptr); printf("After increment address in char_ptr = %u\n", char_ptr); printf("After increment address in float_ptr = %u\n\n", float_ptr);
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]= 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];
Now we can access every value of x using p++ to move from element to another. p=&x[0]= p+1=&x[1]= p+2=&x[2]= p+3=&x[3]= p+4=&x[4]= The address of an element is calculated using its index and the scale factor of the data type. address of x[3]=base address+(3scale factor of int) =1000+(32)= 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];
return (x); else return (y); }
return (p); else return (q); }
Dynamic Memory Management Functions (or) Dynamic Memory Allocation: We can allocate memory to variables in two ways.
**1. Static Memory Allocation.
for(i=0;i<n;i++) scanf(”%d”p+i); printf(“The Elements are”); for(i=0;i<n;i++) printf(“%d”,*(p+i)); getch(); } (ii) calloc: This function is used to allocate memory at run time.It is called with two parameter,the 1 st^ one is an unsigned element specifying the number of objects for which you want to allocate space,the 2nd^ parameter specifies the size of each element. Syntax: ptrvariable=(datatype *)calloc(number of data items,size); Ex: ptr=(int *)calloc(10,2); The main difference between malloc & calloc is that,the calloc initializes the allocated space to zeros;malloc does not provide any initialization. The main difference between malloc & calloc is that by default the memory is allocated by malloc() contains garbage values;where as that which allocated by calloc() contains all zeros. To use these function,the file include ‘alloc.h’ at the beginning of the program. (iii)realloc: It is possible to increase or decrease already allocated memory by using this reallocation function.i.e;when once we allocate memory dynamically through malloc or calloc,the allocated block size can be increased or decreased by this function. ptrvariable=(datatype *)realloc(starting address of allocated memory,new byte size required); Ex : ptr=(int *)realloc(ptr,40); (iv)free(): The memory once allocated can be deallocated using this function. Syntax : free (Starting address of allocated block); Ex : free(ptr);