[go: up one dir, main page]

0% found this document useful (0 votes)
17 views18 pages

Pointers

The document provides an overview of pointers in C programming, explaining their definition, advantages, and usage. It covers topics such as pointer variables, pointer arithmetic, null pointers, generic pointers, and passing arguments to functions using pointers. Additionally, it discusses the relationship between pointers and arrays, as well as how to manipulate strings using pointers.

Uploaded by

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

Pointers

The document provides an overview of pointers in C programming, explaining their definition, advantages, and usage. It covers topics such as pointer variables, pointer arithmetic, null pointers, generic pointers, and passing arguments to functions using pointers. Additionally, it discusses the relationship between pointers and arrays, as well as how to manipulate strings using pointers.

Uploaded by

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

POINTERS:

One of the powerful features of C is ability to access the memory


variables by their memory address.

This can be done by using Pointers.

A pointer is a variable that can store an address of a variable (i.e.,


133200).
We say that a pointer points to a variable that is stored at that
address.

Advantages of Pointers:

1. A pointer enables us to access a variable that is defined out side


the function.
2. Pointers are more efficient in handling the data tables.
3. Pointers reduce the length and complexity of a program.
4. They increase the execution speed.

A variable that holds a physical memory address is called a pointer


variable or Pointer.

data_type *ptr_name;

Eg:- int *pnum; /* pointer to int */


char *pch; /* pointer to char */
float *pfnum; /* pointer to float */
char **s; /* pointer to variable that is a pointer to char */
Example:
#include<stdio.h>
main()
{
int *pnum;
char *pch;
float *pfnum;
double *pdnum;
long *plnum;
printf (“\n Size of integer pointer = %d”, sizeof(pnum));
printf (“\n Size of character pointer = %d”, sizeof(pch));
printf (“\n Size of float pointer = %d”, sizeof(pfnum));
printf (“\n Size of double pointer = %d”, sizeof(pdnum));
printf (“\n Size of long pointer = %d”, sizeof(plnum));
}
Size of integer pointer = 2 OR 8
Size of integer pointer = 2
Size of integer pointer = 2
Size of integer pointer = 2
Size of integer pointer = 2

In this case , all different data type ,occupy the same amount of
space in memory but how much space they occupy will depend on
the platform where the code is going to run.

So, if we declared,
int x = 10;
int *ptr;
ptr = &x;

Memory representation:
Now illustrate this,

Assume that
int i = 3 ;

This declaration tells the C compiler to:

(a) Reserve space in memory to hold the integer value.

(b) Associate the name i with this memory location.

(c) Store the value 3 at this location.

Print this address number through the following program:


main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3

From above example:

A pointer is a variable that contains an address which is a location of


another variable in memory.
The expression &i gives the address of the variable i. This address
can be collected in a variable, by saying,
Consider the Statement

j = &i ;

Here “&” is called address of a variable.


“j” contains the address of a variable i.

j is not an ordinary variable like any other integer variable. It is a


variable that contains the address of other variable (i in this case).
Since j is a variable the compiler must provide it space in the
memory.
As you can see,

i’s value is 3 and j’s value is i’s address.

But, we can’t use j in a program without declaring it. And since j is a


variable that contains the address of i, it is declared as,

int *j ;
This declaration tells the compiler that j will be used to store the
address of an integer value. In other words j points to an integer.

The operator & returns the memory address of variable on which it


is operated, this is called Referencing.
The * operator is called an indirection operator or dereferencing
operator which is used to display the contents of the Pointer
Variable.
Example:-
main( )
{
int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3

Note that printing the value of *( &i ) is same as printing the value
of i.
Example:-
main( )
{
int i = 3 ;
int *j ;
j = &i ; OUTPUT
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}
Consider the following Statements :
int *p, x;
x =5;
p= &x;

Assume that x is stored at the memory address 3000.


Then the output for the following printf statements is :
Output
printf(“The Value of x is %d”,x);

printf(“The Address of x is %u”,&x);

printf(“The Address of x is %u”,p);

printf(“The Value of x is %d”,*p);

printf(“The Value of x is %d”,*(&x));


A pointer that contains another pointer’s address
Pointers to Pointers
Consider the Statement: A pointer variable that is
int i = 3; declared but not initialized
int *j; contains garbage value. Hence
int **k; a pointer variable must not be
j = &i ; used before it is assigned any
variable’s address.
k = &j ;
Here, i is an ordinary int,
j is a pointer to an int (an integer pointer),
whereas k is a pointer to an integer pointer.

Example:-
main( )
{
nt i = 3, *j, **k ;
j = &i ;
k = &j ; OUTPUT
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of i = %u", *k ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nAddress of j = %u", k ) ;
printf ( "\nAddress of k = %u", &k ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of k = %u", k ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", * ( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}

Pointer Expressions and Pointer Arithmetic:

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;

++ operator has greater operator


*ptr++ *(ptr++) precedence than *

This expression will increase the value of ptr, so that it now points to
the next memory location.

If you want to increment the value of the variable whose address is


store in ptr. Then

(*ptr) ++
NULL Pointers:
Null pointer which is a special pointer that does not point to any
value.
This means that a null pointer does not point to any valid memory
address.

int *ptr = NULL;


Or
int ptr;
ptr = 0;
#include<stdio.h> or <stdlib.h> or <string.h>
int main()
{
int *ptr = NULL;
printf(“\n the valu of ptr is = %x”, ptr);
return 0;
}

GENERIC Pointer:
A generic pointer is a pointer variable that has void as its data type.
A void pointer or the generic pointer is a special type of pointer that
can be used to point to variables of any data type.

void *ptr;

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 point to the character = %c”, *(char*) gp);
rerurn 0;
}

Passing arguments to function using POINTERS:

‘Call by Value’ example

main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapv ( int x, int y )
{
int t ;
t=x;
x=y;
y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}

x = 20 y = 10
a = 10 b = 20

Note that values of a and b remain unchanged even after


exchanging the values of x and y.
In the second method (call by reference) the addresses of actual
arguments in the calling function are copied into formal arguments
of the called function. This means that using these addresses we
would have an access to the actual arguments and hence we would
be able to manipulate them.

main( )
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapr( int *x, int *y )
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}

a = 20 b = 10

Note that this program manages to exchange the values of a and b


using their addresses stored in x and y.
Example:
Add two integers using functions pointer.

#include<stdio.h>
Pointer and Arrays:
The concept of array is very much bound to the one of the pointers.
An array occupies consecutive memory locations.
For example: int arr [ ] = {1, 2, 3, 4, 5};
1 2 3 4 5
arr[0] arr[1] arr[2] arr[3] arr[4]
2000 2002 2004 2006 2008
If arr and arr[0] points to location 2000. If we declare p as an integer
pointer, then we can make the pointer P to point to the array a by
following assignment,
P = arr;
We can access every value of array a by moving P from one element
to another.
i.e.,
P points to 0th element
P+1 points to 1st element, P+2 points to 2nd element, P+3 points to
3rd element, P +4 points to 4th element,

Array notation is a form of pointer notation.


So, base address is the address of the first element in the array or
the address of arr[ 0 ].
int *ptr; %p control string prints the
ptr = &arr[0]; argument as a memory address in
Eg. main() hexadecimal form.
{ %u prints memory address in
int arr[] = {1,2,3,4,5}; decimal form.
printf(“\n Address of array = %p %p %p”, arr, &arr[0], &arr);
}
If writing , ptr = &arr[2];
So, it makes ptr to point to the third element of the array that has
index 2.
1 2 3 4 5
arr[0] arr[1] arr[2] arr[3] arr[4]

ptr
Array of Pointers(Pointer arrays):

An array of pointers is an indexed set of variables in which the


variables are pointers (a reference to a location in memory).

Syntax:
datatype *arr_name[ size ];
Example:
int *ptr[5];
Here ptr is an array name whose elements are integer pointers
Program for Array of Pointers

#include<stdio.h>
int main( )
{
int a=10, b=20, c=30; int *ptr[3];
ptr[0]=&a;
ptr[1]=&b;
ptr[2]=&c;
printf(“Value of a : %d”,*ptr[0]);
printf(“Value of b : %d”,*ptr[1]);
printf(“Value of c : %d”,*ptr[2]);
return 0;
}
Strings and Pointers:
A pointer can be used to access the individual elements of a String.
For Example
char str[10];
declares a string str. The variable name of the string str holds the
address of the first element of the array i.e., it points at the starting
memory address.
So, we can create a character pointer ptr and store the address of
the string str variable in it. This way, ptr will point at the string str.
char *ptr= str;
In the following code we are assigning the address of the string str to
the pointer ptr.

#include<stdio.h>
int main()
{
char str[6] = "Hello"; // string variable
char *ptr = str; //pointer variable points to the first address of the string
// print the contents of the string using pointer
while(*ptr!='\0')
{
printf("%c", *ptr);
ptr++; // increment ptr so that it points to the next element of the string
}
return 0;
}

You might also like