0% found this document useful (0 votes)
106 views16 pages

Pointers

Pointers are variables that store memory addresses of other variables. They allow passing information between functions, returning multiple values from functions, and accessing array elements indirectly. Pointers are declared with a data type followed by an asterisk, and are assigned the address of another variable using the address-of operator (&). They can be dereferenced using the asterisk (*) operator to access the value of the variable being pointed to. Pointers enable passing data between functions by reference rather than by value.

Uploaded by

Ruchita Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views16 pages

Pointers

Pointers are variables that store memory addresses of other variables. They allow passing information between functions, returning multiple values from functions, and accessing array elements indirectly. Pointers are declared with a data type followed by an asterisk, and are assigned the address of another variable using the address-of operator (&). They can be dereferenced using the asterisk (*) operator to access the value of the variable being pointed to. Pointers enable passing data between functions by reference rather than by value.

Uploaded by

Ruchita Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Pointers

What are pointers?


• int x = 10;
• When this statement executes, the compiler sets aside 4 bytes of memory to hold the value 10. It also sets up
a symbol table in which it add the symbol x and the relative address in the memory where 4 bytes are set
aside
• Every variable in C has a value and also a memory location (commonly known as address) associated with it.
• A pointer is a variable that contains the memory location of another variable. Therefore, a pointer is a
variable that represents the location of a data item, such as a variable or an array element.
Applications of Pointers
• Pointers are used to pass information back and forth between functions.
• Pointers enable the programmers to return multiple data items from a function via function arguments.
• Pointers provide an alternate way to access the individual elements of an array.
• Pointers are used to pass arrays and strings as function arguments.
• Pointers are used to create complex data structures, such as trees, linked lists, linked stacks, linked queues,
and graphs.
• Pointers are used for the dynamic memory allocation of a variable
Declaring Pointer Variables
• Syntax:
• data_type *ptr_name;
• data_type is the data type of the value that the pointer will point to. For example,
• int *pnum;
• char *pch;
• float *pfnum;
• Although all these pointers (pnum, pch, and pfnum) point to different data types, they will occupy the same
amount of space in the memory.
Declaring pointer variables
• Declaring an integer pointer variable
int x= 10;
int *ptr;
ptr = &x;
• ptr is the name of the pointer variable. The * informs the compiler that ptr is a pointer variable and the
int specifies that it will store the address of an integer variable.
• An integer pointer variable, therefore, ‘points to’ an integer variable.
• ptr is assigned the address of x. The & operator retrieves the lvalue (address) of x, and copies that to the
contents of the pointer ptr.
Declaring pointer variables

• Since x is an integer variable, it will be allocated 2 bytes or 4 bytes based on the system
used.
• Assuming that the compiler assigns it memory locations 1003 and 1004, the address of x
(written as &x) is equal to 1003, that is the starting address of x in the memory. When we
write, ptr = &x, then ptr = 1003.
• We can ‘dereference’ a pointer, i.e., we can refer to the value of the variable to which it
points by using the unary * operator as in *ptr. That is, *ptr = 10, since 10 is the value of
x.
#include <stdio.h>
int main()
{
int num, *pnum;
pnum = &num;
printf("\n Enter the number : ");
scanf("%d", &num);
printf("\n The number that was entered is : %d", *pnum);
return 0;
}
Output
Enter the number : 10
The number that was entered is : 10

What will be the value of *(&num)?


It is equivalent to simply writing num.
Pointer Expressions and Pointer Arithmetic
• Pointer variables can also be used in expressions. For example, if ptr1 and ptr2 are pointers, then the
following statements are valid:
• int num1 = 2, num2 = 3, sum = 0, mul = 0, div = 1;
• int *ptr1, *ptr2;
• ptr1 = &num1;
• ptr2 = &num2;
• sum = *ptr1 + *ptr2;
• mul = sum * (*ptr1);
• *ptr2 += 1;
• div = 9 + (*ptr1)/(*ptr2) – 30;
• The programmer may add integers to or subtract integers from pointers as well as subtract one pointer from
the other. We can also use shorthand operators with the pointer variables.
• C also allows comparing pointers by using relational operators in the expressions. For example, p1 > p2,
p1 == p2 and p1! = p2 are all valid in C
Pointer Expressions and Pointer Arithmetic
• Postfix unary increment (++) and decrement (––) operators have greater precedence than the dereference
operator (*).
• Therefore, the expression *ptr++ is equivalent to *(ptr++), as ++ has greater operator precedence than *.
Thus, the expression will increase the value of ptr so that it now points to the next memory location.
• Therefore, to increment the value of the variable whose address is stored in ptr, you should write (*ptr)++.
Null Pointers
• In some cases, we may prefer to have a null pointer which is a special pointer value and does not point to any value. This means that a null
pointer does not point to any valid memory address.
• To declare a null pointer, you may use the predefined constant NULL which is defined in several standard header files including <stdio.h>,
<stdlib.h>, and <string.h>.
• After including any of these files in your program, you can write
• int *ptr = NULL;
• You can always check whether a given pointer variable stores the address of some variable or contains NULL by writing,
if (ptr == NULL)
{
Statement block;
}
• You may also initialize a pointer as a null pointer by using the constant 0
int *ptr,
ptr = 0;
• This is a valid statement in C as NULL is a preprocessor macro, which typically has the value or replacement text 0.
• However, to avoid ambiguity, it is always better to use NULL to declare a null pointer.
• A function that returns pointer values can return a null pointer when it is unable to perform its task.
Generic Pointers
• A generic pointer is a pointer variable that has void as its data type.
• The void pointer, or the generic pointer, is a special type of pointer that can point to variables of any data
type.
• It is declared like a normal pointer variable but using the void keyword as the pointer’s data type.
• For example,
• void *ptr;

• In C, since you cannot have a variable of type void, the void pointer will therefore not point to any data and,
thus, cannot be dereferenced.
• You need to cast a void pointer to another kind of pointer before using it.
• Generic pointers are often used when you want a pointer to point to data of different types at different times.
• It is always recommended to avoid using void pointers unless absolutely necessary, as they effectively allow
you to avoid type checking.
Generic Pointers
#include <stdio.h>
int main()
{
int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d", *(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character= %c", *(char*)gp);
return 0;
}
• Output
• Generic pointer points to the integer value = 10
• Generic pointer now points to the character = A
Passing arguments to functions using pointers
• Pointers provide a mechanism to modify data declared in one
function using code written in another function.
• If data is written in fuc1() and we want to write code in func2() that
modifies the data in func1(), then we must pass the addresses of the
variables to func2().
• The calling function sends the addresses of the variables and the
called function declares those incoming arguments as pointers.
• Inorder to modify the variables sent by the calling function, the called
function must dereference the pointers that were passed to it.
• Thus, passing pointers to a function avoids overhead of copying data
from one function to another function.
Passing arguments to functions using pointers
• To use pointers for passing arguments to a function, the programmer
must do the following:
• Declare the function parameters as pointers.
• Use the dereferenced pointers in the function body.
• Pass the addresses as the actual argument when the function is called.
Write a program to add two integers using pointers and functions
#include <stdio.h>
void sum (int*, int*, int*);
int main()
{
Output
Enter the first number : 23
int num1, num2, total; Enter the second number : 34
printf("\n Enter the first number : "); Total = 57
scanf("%d", &num1);
printf("\n Enter the second number : ");
scanf("%d", &num2);
sum(&num1, &num2, &total);
printf("\n Total = %d", total);
return 0;
}
void sum (int *a, int *b, int *t)
{
*t = *a + *b;
}
Drawbacks of pointers
• If used incorrectly, pointers can lead to bugs that are difficult to resolve.
• For example, if you use a pointer to read a memory location but that pointer is pointing to an incorrect
location, then you may end up reading a wrong value. An erroneous input always leads to an erroneous
output. Thus however efficient your program code may be, the output will always be disastrous. Same is the
case when writing a value to a particular memory location.

You might also like