0% found this document useful (0 votes)
320 views8 pages

Pointers

Pointer variables store the memory addresses of other variables. There are two pointer operators in C - the address of operator (&) returns the memory address of a variable, and the value at operator (*) accesses the value of the variable at a given memory address. Pointers allow dynamic memory allocation, passing arguments by reference, and accessing array elements by treating arrays as pointers to their initial elements.

Uploaded by

tovilas
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)
320 views8 pages

Pointers

Pointer variables store the memory addresses of other variables. There are two pointer operators in C - the address of operator (&) returns the memory address of a variable, and the value at operator (*) accesses the value of the variable at a given memory address. Pointers allow dynamic memory allocation, passing arguments by reference, and accessing array elements by treating arrays as pointers to their initial elements.

Uploaded by

tovilas
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/ 8

Pointer: Fundamentals, Pointer variables, Referencing and de-referencing,

Pointer Arithmetic, Using Pointers with Arrays, Using Pointers with Strings,
Array of Pointers, Pointers as function arguments, Functions returning
pointers.

Q.) What is pointer in C language? Explain the declaration of pointer variable.


Pointer in C language is a variable that stores/points the address of another variable. A Pointer
in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be
belonging to any of the data type such as int, float, char, double, short etc.

Syntax(pointer type declaration)::


data_type *var_name;

Example: int *p; char *ptr;


Where, * is used to denote that p and ptr are pointer variables and not a normal variables
which will store addresses of other integer and character type of variables respectively.

Q.) Explain use of pointers address operators


There are two operators used in C language to handle pointers.

(a) Address of (&) operator:


Address-of operator points to the location in the memory because the value of the pointer is
the memory address/location where the data item resides in memory. To access address of a
variable to a pointer, we use the unary operator & (ampersand) that returns the address of
that variable. Example &num will access the address of variable num.

Point to note: %p is a format specifier which is used for displaying the address in hex
format.

(b) Value at (*) Asterisk operator:


This operator is used for two things (i) to declare a pointer variable that stores an address of
another variable. (ii) to access value of a variable through a pointer.

Example:
int *p1 /*Pointer to an integer variable*/
double *p2 /*Pointer to a variable of data type double*/
char *p3 /*Pointer to a character variable*/
float *p4 /*pointer to a float variable*/

The above are the few examples of pointer declarations. If you need a pointer to store the
address of integer variable then the data type of the pointer should be int. Same case is with
the other data types.

For example:
int var = 10;
int *ptr;
p = &var;

*ptr would give us the value of the variable a.


The following statement would display 10 as output.
printf("%d", *ptr);

Similarly if we assign a value to *pointer like this:


*ptr = 100;
It would change the value of variable a. The statement above will change the value of an
variable a from 10 to 100.

Ex-1) EXAMPLE PROGRAM FOR POINTERS IN C:


#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
OUTPUT:
50

Ex-2) Example of Pointer demonstrating the use of & and *


#include <stdio.h>
int main()
{
int *p;
int var = 10;
p= &var;

printf("Value of variable var is: %d", var);


printf("\nValue of variable var is: %d", *p);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", p);
printf("\nAddress of pointer p is: %p", &p);
return 0;
}
OUTPUT:
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50

Reference operator(&):
Address of operator (“&”) is known as referencing operator.
This operator returns the address of the variable associated with the operator.
For e.g., if we write “&x”, it will return the address of the variable “x’.
Hence, if we have a pointer “p”, which we want to point to a variable x, then we need to
copy the address of the variable “x” in the pointer variable “p”.
This is implemented by the statement: p = &x;

Dereference operator(*):
Value of operator (“*”) is known as dereference operator.
This operator returns the value stored in the variable pointed by the specified pointer.
For e.g., if we write “*p”, it will return the value of the variable pointed by the pointer “p”.
Hence, if we want the value of the variable pointed by the pointer “p” to be stored in a
variable “y”, then the expression can be written as: y = *p;

Pointer Arithmetic
A pointer in c is an address, which is a numeric value. Therefore, you can perform
arithmetic operationson 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 address1000. Assuming 16-bit integers, let us perform the following
arithmetic operation on the pointer

ptr++

After the above operation, the ptr will point to the location 1002 because each time ptr
is incremented, itwill point to the next integer location which is 2 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.

Following is a table for size of datatypes on 16-bit Machine:


Type Size (in bytes)
int 2
char 1
long 4
float 4
double 8
Pointer to array:
Address of first value of an array is assigned to the pointer variable and then array
elements can beaccessed through pointer. The following program increments the
variable pointer to access each succeeding element of the array.

Consider the following program:

#include<stdio.h>
int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
int *ptr = arr;

printf("%p\n", ptr);return 0;
}

In this program, we have a pointer ptr that points to the 0th element of the array

As studied above, we can use a pointer to point to an array, and then we can use that
pointer to access thearray elements

Lets have an example,#include <stdio.h>


int main()
{
int i;
int a[5] = {10, 20, 30, 40, 50};
int *ptr = a; // same as int *p = &a[0]

for (i = 0; i < 5; i++)


{
printf("%d ", *p); // Print value of an array
p++; // pointer will point next element of an array
}
return 0;
}

OUTPUT:
10 20 30 40 50

In the above program, the pointer *ptr will print all the values stored in the array one by
one. We can alsouse the Base address (a in above case) to act as a pointer and print all
the values. Base address is an address of a first value stored in an array.
Pointers as Function Argument in C (Pass by Reference):
Pointer as a function parameter is used to hold addresses of arguments passed during
functioncall. This is also known as call by reference. When a function is called by
reference any change made to the reference variable will affect the original variable.

Example: Swapping two numbers using Pointer


#include <stdio.h>

void swap(int *a,

int *b);int main()


{
int m = 10, n
= 20;clrscr();
printf("Before Swapping:\n\n");

printf("m = %d\n",
m);printf("n
= %d\n\n", n);

swap(&m, &n); //passing address of m and n to the swap


functionprintf("After Swapping:\n\n");
printf("m = %d\n", m);
printf("n = %d", n);return 0;
}

/*
pointer 'a' and 'b' holds and
points to the address of 'm'
and 'n'
*/
void swap(int *a, int *b)
{
int
temp;
temp
= *a;
*a = *b;
*b = temp;
}

Output:
Pointer to functions
A pointer to a function points to the address of the executable code (Function
Definition) ofthe function. Following is a simple example that shows declaration and
function call using function pointer.,

Syntax:
data_type (*pointer-name)(parameter);

Here is an example :
int (*sum)(); //legal declaration of pointer to function
int *sum(); //This is not a declaration of pointer to function

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

function.int sum (int, int);


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

Here is a pointer to a function sum. Now sum can be called using function pointer s along
withproviding the required argument values.

s (10, 20);

Example of Pointer to Function


#include <stdio.h>

int sum (int, int); //Function proto/declaration


int main( )
{
int (*fp)(int, int); // data_type (*pointer-name)(parameter);
fp = sum;

int s = fp(10, 15);


printf("Sum
is %d", s);

return 0;
}

int sum(int x, int y)


{
return x+y;
}

Output:
25
Function returning Pointer:
Pointers in C programming language is a variable which is used to store the memory address
of another variable. We can pass pointers to the function as well as return pointer from a
function. But it is not recommended to return the address of a local variable outside the
function as it goes out of scope after function returns.

Program 1:
The below program will give segmentation fault since ‘A’ was local to the function

// C program to illustrate the concept of returning pointer from a function

#include <stdio.h>

// Function returning pointer


int* fun()
{
int A = 10;
return (&A);
}

// Driver Code
int main()
{
// Declare a pointer
int* p;

// Function call
p = fun();

printf("%p\n", p);
printf("%d\n", *p);
return 0;
}

Output:
ERROR
Explanation:
The main reason behind this scenario is that compiler always make a stack for a
function call. As soon as the function exits the function stack also gets removed
which causes the local variables of functions goes out of scope.
Static Variables have a property of preserving their value even after they are out of
their scope. So to execute the concept of returning a pointer from function in C you
must define the local variable as a static variable.
Program 2:
// C program to illustrate the concept of
// returning pointer from a function
#include <stdio.h>

// Function that returns pointer


int* fun()
{
// Declare a static integer
static int A = 10;
return (&A);
}

// Driver Code
int main()
{
// Declare a pointer
int* p;

// Function call
p = fun();

// Print Address
printf("%p\n", p);

// Print value at the above address


printf("%d\n", *p);
return 0;
}

Output:
0x601038
10

You might also like