0% found this document useful (0 votes)
13 views

POINTER

Pointers in C are variables that store the address of another variable, providing flexibility and efficiency in handling data structures and memory management. They enable operations like dynamic memory allocation, function arguments passing by reference, and pointer arithmetic. Understanding pointers is essential for manipulating data effectively in C programming.

Uploaded by

ravalkaran187
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)
13 views

POINTER

Pointers in C are variables that store the address of another variable, providing flexibility and efficiency in handling data structures and memory management. They enable operations like dynamic memory allocation, function arguments passing by reference, and pointer arithmetic. Understanding pointers is essential for manipulating data effectively in C programming.

Uploaded by

ravalkaran187
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/ 16

Introduction to Pointers

Pointers are variables that hold address of another variable of same data type.

Pointers are one of the most distinct and exciting features of C language. It provides power
and flexibility to the language.

A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, you must declare a pointer before using it
to store any variable address. The general form of a pointer variable declaration is −

type *var-name;

Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer. Take a look at some of the valid pointer declarations −

int *ip; /* pointer to an integer */


double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */

The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address. The
only difference between pointers of different data types is the data type of the variable or
constant that the pointer points to.

Features/Advantages –

1) Pointers are more efficient in handling arrays and data tables.


2) It can be used to return multiple values from a function.
3) Pointers permits reference to functions and thereby passing of function as argument
to other function
4) The use of pointer arrays to character strings results in saving of data storage space
in memory.
5) Pointer allows to support dynamic memory allocation using malloc, calloc, realloc &
free().
6) Pointer provide an efficient tool for manipulating data structures such as stack,
queue, structure, linked list and trees.
7) Pointer reduces the length and complexity of program.
8) They increase the execution speed and thus reduce the program execution time.

Concept of Pointer

Whenever a variable is declared, system will allocate a location to that variable in the
memory, to hold value. This location will have its own address number.

Let us assume that system has allocated memory location 80F for a variable a.
int a = 10 ;

We can access the value 10 by either using the variable name a or the address 80F. Since the
memory addresses are simply numbers they can be assigned to some other variable. The
variable that holds memory address are called pointer variables. A pointer variable is
therefore nothing but a variable that contains an address, which is a location of another
variable. Value of pointer variable will be stored in another memory location.

Reference operator(&)

If var is a variable then, &var is the address in memory.

/* Example to demonstrate use of reference operator in C programming. */


#include <stdio.h>
int main(){
int var=5;
printf("Value: %d\n",var);
printf("Address: %d",&var); //Notice, the ampersand(&) before var.
return 0;
}

Output

Value: 5
Address: 2686778

Dereference operator(*) and Pointer variables

Pointer variable or simply pointer are the special types of variables that holds memory
address rather than data, that is, a variable that holds address value is called a pointer variable
or simply a pointer.

Declaration of Pointer

Dereference operator(*) are used for defining pointer variable

data_type* pointer_variable_name;
int* p;

Above statement defines, p as pointer variable of type int.


Once a pointer has been assigned the address of a variable. To access the value of variable,
pointer is dereferenced, using the indirection operator *.

int a,*p;
a = 10;
p = &a;

printf("%d",*p); //this will print the value of a.

printf("%d",*&a); //this will also print the value of a.

printf("%u",&a); //this will print the address of a.

printf("%u",p); //this will also print the address of a.

printf("%u",&p); //this will also print the address of p.

How to use Pointers?

There are few important operations which we will do with the help of pointers very
frequently.

(a) We define a pointer variable

(b) Assign the address of a variable to a pointer and

(c) Finally access the value at the address available in the pointer variable. This is done by
using unary operator * that returns the value of the variable located at the address specified
by its operand. Following example makes use of these operations:

#include <stdio.h>

int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */


printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */


printf("Value of *ip variable: %d\n", *ip );

return 0;
}

When the above code is compiled and executed, it produces result something as follows:

Address of var variable: bffd8b3c


Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
Pointer Flexibility –

Pointers are flexible. We can make the same pointer to point to different data variable in
different statements. Example

int x,y,z,*ptr;

ptr=&x;

ptr=&y;

ptr=&z;

We can also use the different pointer to point to the same data variable. For example

int x;

int *p1=&x;

int *p2=&x;

int *p3=&x;

Chain of Pointer –

It is possible to make a pointer to point to another pointer, thus creating a chain of pointers
as follows –

P2-------p1---------------variable’s value

Here, the pointer variable p2 contains the address of the pointer variable p1, which points
to the location that contain the desired value. This is known as “MULTIPLE INDIRECTION”

A variable that is pointer to a pointer must be declared using additional indirection operator
symbols in front of the name example –

int **p2;

This declaration tells the compiler that p2 is a pointer to pointer of int type. Remember, the
pointer p2 is not a pointer to an int, but rather a pointer to an int pointer.

Pointer Increment and scale factor –

The pointer can incremented like –

p1=p1+2;

p1=p1+10;

But expression like –


P1++;

will cause the pointer p1 to point to the next value of its type. For example if p1 is an integer
pointer with an initial value, say 2000 then after the operation p1=p1+1, the value of p1 will
be 2002 and not 2001.

It means, when we increment a pointer, its value is increase by the length of the data type
that it points to. This length is called the SCALE FACTOR.

Pointer as function argument –

The process of calling function using a pointer to pass the addresses of variable is known as
“call by reference”.

And the process of passing the actual value of variable is known as “Call by value”.

Following things need to satisfy while doing the same-

1) The function parameters are declared as pointer.


2) The dereference pointer are used in the function body.
3) When the function is called, the addresses are passed as actual argument.
 Functions returning pointers –

Example –

Int *large(int *,int *); //function prototype

main()

int a = 10, b = 20;

int *p;

p = large(&a,&b); //function call

printf(“%d”,*p);

int *large(int *x,int *y) //function definition

If(*x>*y)

return(x); //address of a

else

return(y); //address of b

}
NULL Pointers in C

It is always a good practice to assign a NULL value to a pointer variable in case you do not
have exact address to be assigned. This is done at the time of variable declaration. A pointer
that is assigned NULL is called a null pointer.

The NULL pointer is a constant with a value of zero defined in several standard libraries.
Consider the following program:

#include <stdio.h>

int main ()
{
int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;
}

When the above code is compiled and executed, it produces following result:

The value of ptr is 0

C - Pointer arithmetic

A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic
operations on a pointer just as you can on a numeric value. There are four arithmetic
operators that can be used on pointers: ++, --, +, and -

To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to
the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation
on the pointer −

ptr++

After the above operation, the ptr will point to the location 1004 because each time ptr is
incremented, it will point to the next integer location which is 4 bytes next to the current
location. This operation will move the pointer to the next memory location without impacting
the actual value at the memory location. If ptr points to a character whose address is 1000,
then the above operation will point to the location 1001 because the next character will be
available at 1001.

Incrementing a Pointer

We prefer using a pointer in our program instead of an array because the variable pointer can
be incremented, unlike the array name which cannot be incremented because it is a constant
pointer. The following program increments the variable pointer to access each succeeding
element of the array −
#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have array address in pointer */


ptr = var;

for ( i = 0; i < MAX; i++) {

printf("Address of var[%d] = %x\n", i, ptr );


printf("Value of var[%d] = %d\n", i, *ptr );

/* move to the next location */


ptr++;
}

return 0;
}

When the above code is compiled and executed, it produces the following result −

Address of var[0] = bf882b30


Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200

Decrementing a Pointer

The same considerations apply to decrementing a pointer, which decreases its value by the
number of bytes of its data type as shown below −

#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have array address in pointer */


ptr = &var[MAX-1];

for ( i = MAX; i > 0; i--) {

printf("Address of var[%d] = %x\n", i, ptr );


printf("Value of var[%d] = %d\n", i, *ptr );

/* move to the previous location */


ptr--;
}

return 0;
}
When the above code is compiled and executed, it produces the following result −

Address of var[3] = bfedbcd8


Value of var[3] = 200
Address of var[2] = bfedbcd4
Value of var[2] = 100
Address of var[1] = bfedbcd0
Value of var[1] = 10

Pointer Comparisons

Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2
point to variables that are related to each other, such as elements of the same array, then p1
and p2 can be meaningfully compared.

The following program modifies the previous example − one by incrementing the variable
pointer so long as the address to which it points is either less than or equal to the address of
the last element of the array, which is &var[MAX - 1] −

#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have address of the first element in pointer */


ptr = var;
i = 0;

while ( ptr <= &var[MAX - 1] ) {

printf("Address of var[%d] = %x\n", i, ptr );


printf("Value of var[%d] = %d\n", i, *ptr );

/* point to the previous location */


ptr++;
i++;
}

return 0;
}

When the above code is compiled and executed, it produces the following result −

Address of var[0] = bfdbcb20


Value of var[0] = 10
Address of var[1] = bfdbcb24
Value of var[1] = 100
Address of var[2] = bfdbcb28
Value of var[2] = 200

Array of pointers
#include <stdio.h>

const int MAX = 3;


int main () {

int var[] = {10, 100, 200};


int i;

for (i = 0; i < MAX; i++) {


printf("Value of var[%d] = %d\n", i, var[i] );
}

return 0;
}

When the above code is compiled and executed, it produces the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

There may be a situation when we want to maintain an array, which can store pointers to an
int or char or any other data type available. Following is the declaration of an array of
pointers to an integer −

int *ptr[MAX];

It declares ptr as an array of MAX integer pointers. Thus, each element in ptr, holds a pointer
to an int value. The following example uses three integers, which are stored in an array of
pointers, as follows −

#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr[MAX];

for ( i = 0; i < MAX; i++) {


ptr[i] = &var[i]; /* assign the address of integer. */
}

for ( i = 0; i < MAX; i++) {


printf("Value of var[%d] = %d\n", i, *ptr[i] );
}

return 0;
}

When the above code is compiled and executed, it produces the following result −

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

Pointer to Structure

Like we have array of integers, array of pointer etc, we can also have array of structure
variables. And to make the use of array of structure variables efficient, we use pointers of
structure type. We can also have pointer to a single structure variable, but it is mostly used
with array of structure variables.

struct Book
{
char name[10];
int price;
}

int main()
{
struct Book a; //Single structure variable
struct Book* ptr; //Pointer of Structure type
ptr = &a;

struct Book b[10]; //Array of structure variables


struct Book* p; //Pointer of Structure type
p = &b;
}

Accessing Structure Members with Pointer

To access members of structure with structure variable, we used the dot . operator. But when
we have a pointer of structure type, we use arrow -> to access structure members.

struct Book
{
char name[10];
int price;
}

int main()
{
struct Book b;
struct Book* ptr = &b;
ptr->name = "Dan Brown"; //Accessing Structure Members
ptr->price = 500;
}

Pointer as Function parameter

Pointer in function parameter list is used to hold address of argument passed during function
call. This is also known as call by reference. When a function is called by reference any
change made to the reference variable will effect the original variable.
Example: Sorting an array using Pointer

#include <stdio.h>
#include <conio.h>
void sorting(int *x, int y);
void main()
{
int a[5],b,c;
clrscr();
printf("enter 5 nos");
for(b=0; b<5; b++)
{
scanf("%d",&a[b]);
}
sorting(a, 5);
getch();
}

void sorting(int *x, int y)


{
int i,j,temp;
for(i=1; i<=y-1; i++)
{
for(j=0; j*(x+j+1))
{
temp=*(x+j);
*(x+j)=*(x+j+1);
*(x+j+1)=temp;
}
}
}
for(i=0; i<5; i++)
{
printf("\t%d",*(x+i));
}
}

Function returning Pointer

A function can also return a pointer to the calling function. In this case you must be careful,
because local variables of function doesn't live outside the function, hence if you return a
pointer connected to a local variable, that pointer be will pointing to nothing when function
ends.

#include <stdio.h>
#include <conio.h>
int* larger(int*, int*);
void main()
{
int a=15;
int b=92;
int *p;
p=larger(&a, &b);
printf("%d is larger",*p);
}

int* larger(int *x, int *y)


{
if(*x > *y)
return x;
else
return y;
}

Safe ways to return a valid Pointer.

1. Either use argument with functions. Because argument passed to the functions are declared
inside the calling function, hence they will live outside the function called.
2. Or, use static local variables inside the function and return it. As static variables have a
lifetime until main() exits, they will be available througout the program.

Pointer to functions

It is possible to declare a pointer pointing to a function which can then be used as an


argument in another function. A pointer to a function is declared as follows,

type (*pointer-name)(parameter);

Example :
int (*sum)(); //legal declaraction of pointer to function
int *sum(); //This is not a declaraction of pointer to function

A function pointer can point to a specific function when it is assigned the name of the
function.

int sum(int, int);


int (*s)(int, int);
s = sum;

s is a pointer to a function sum. Now sum can be called using function pointer s with the list
of parameter.

s (10, 20);

Example of Pointer to Function

#include <stdio.h>
#include <conio.h>

int sum(int x, int y)


{
return x+y;
}

int main( )
{
int (*fp)(int, int);
fp = sum;
int s = fp(10, 15);
printf("Sum is %d",s);
getch();
return 0;
}

Output : 25

Question : Are *ptr++ and ++*ptr are same ?

Ans : Absolutely Not

*ptr++ means

 Increment the Pointer not Value Pointed by It

++*ptr means

 Increment the Value being Pointed to by ptr

Difference between char *a and char a[]

char * and char [] both are used to access character array, Though functionally both are same
, they are syntactically different. See how both works in order to access string.

A. How Char a[] Works ?

Consider following sample example for storing and accessing string using character array –

char a[] = "HELLO";

In the example – String “Hello” is stored in Character Array ‘a’. Character array is used to
store characters in Contiguous Memory Location. It will take Following Form after
Initialization. We have not specified Array Size in this example.Each array location will get
following values –

a[0] = 'H'
a[1] = 'E'
a[2] = 'L'
a[3] = 'L'
a[4] = 'O'
a[5] = '\0'

It will Looks Like This …


Accessing Individual Element :

Suppose we have to find out a[3] then firstly compiler will check whether ‘a’ is Array or
Pointer ? If ‘a’ is Array Variable then It starts at the location “a”, goes three elements past it,
and returns the character there.In this method element is accessed Sequentially

B. How Char *a Works ?

String “Hello” will be stored at any Anonymous location in the form of array. We even don’t
know the location where we have stored string, However String will have its starting address.

Syntax of Char *a :
char *a = "HELLO";

the above syntax will take following form –

We have declared pointer of type character i.e pointer variable is able to hold the address of
character variable. Now Base address of anonymous array is stored in character pointer
variable. ‘a’ Stores Base Address of the Anonymous Array [Unknown Array]

Address = [Base Address of Anonymous Array] + [i]

Accessing Individual Element :

Consider we have to access a[3] then –


In Short if ‘a’ is a pointer, it starts at the location “a”, gets the pointer value there, adds 3 to
the pointer value, and gets the character pointed to by that value
[box]In both Cases a[3] returns same Character but Procedure is Different.[/box]

Typedef-

The C programming language provides a keyword called typedef which you can use to give a
type a new name. Following is an example to define a term BYTE for one-byte numbers:

typedef unsigned char BYTE;

After this type definitions, the identifier BYTE can be used as an abbreviation for the type
unsigned char, for example:.

BYTE b1, b2;


You can use typedef to give a name to user defined data type as well. For example you can
use typedef with structure to define a new data type and then use that data type to define
structure variables directly as follows:

#include <stdio.h>
#include <string.h>

typedef struct Books


{
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;

int main( )
{
Book b1;

strcpy( b1.title, "C Programming");


strcpy( b1.author, "Nuha Ali");
strcpy( b1.subject, "C Programming Tutorial");
b1.book_id = 6495407;

printf( "Book title : %s\n", b1.title);


printf( "Book author : %s\n", b1.author);
printf( "Book subject : %s\n", b1.subject);
printf( "Book book_id : %d\n", b1.book_id);

return 0;
}
Output-
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407

typedef vs #define

The #define is a C-directive which is also used to define the aliases for various data types
similar to typedef but with three differences:

 The typedef is limited to giving symbolic names to types only where as #define can
be used to define alias for values as well, like you can define 1 as ONE etc.
 The typedef interpretation is performed by the compiler where as #define
statements are processed by the pre-processor.

Following is a simplest usage of #define:

#include <stdio.h>
#define TRUE 1

#define FALSE 0
int main( )
{
printf( "Value of TRUE : %d\n", TRUE);
printf( "Value of FALSE : %d\n", FALSE);
return 0;
}
Value of TRUE : 1
Value of FALSE : 0

self-referential structure-
A self referential structure is used to create data structures like linked lists, stacks, etc.
Following is an example of this kind of structure:

struct struct_name
{
datatype datatypename;
struct_name * pointer_name;
};

A self-referential structure is one of the data structures which refer to the pointer to (points)
to another structure of the same type. For example, a linked list is supposed to be a self-
referential data structure. The next node of a node is being pointed, which is of the same
struct type. For example,

typedef struct listnode {


void *data;
struct listnode *next;
} linked_list;

In the above example, the listnode is a self-referential structure – because the *next is of the
type struct listnode.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy