Pointer C
Pointer C
What is a Pointer?
A pointer is a variable that stores the address of a memory location. Pointers are used to store
the addresses of other variables or memory items. A pointer is extremely helpful for another
type of parameter passing, commonly referred to as pass by address. Pointers are essential for
dynamic memory allocation.
Functions as pointers
Memory stores the function code
The address or start of a function is referred to as a "function pointer"
Since function pointers do not allocate or deallocate memory, they are "different"
from other pointers
It is possible to pass function pointers as arguments to other functions or as returns
from other functions
Why use function pointers?
Efficiency
Elegance
Runtime binding
Function pointer declarations
Unlike a function declaration, a function pointer declaration wraps the function name in
parentheses and precedes it with an asterisk. Here is an example:
int function(int x, int y); /* a function taking two int arguments and returning an int */
int (*pointer)(int x, int y); /* a pointer to such a function */
Pointers as Arguments
By passing a pointer into a function, the function may read or change memory outside of its
activation record.
Example: Pointers and functions
In the following example, function passes the value of p and the address of q, which are used
to initialize the variables x and ptry in the function test.
Code:
#include <stdio.h>
void test( int x, int *ptry )
{
x = 200;
*ptry = 200;
return;
}
int main( void )
{
int p = 10, q = 20;
printf( "Initial value of p = %d and q = %d\n", p, q );
test( p, &q );
printf( "After passes to test function: p = %d, q = %d\n", p, q );
return 0;
}
Copy
In the said code ‘p’ remains at 10 as there is no return value form the function test, but q
change its value from 20 to 200 in the function test() as bptr holds the address of a variable q
that is stored in main function. So when we change the value of *bptr it will automatically
reflecte in the main function.
Output:
Initial value of p = 10 and q = 20
After passes to test function: p = 10, q = 200
Example: Swapping two values using pointers and functions
Code:
#include<stdio.h>
void swap (int *p, int *q) {
int temp_val;
temp_val = *p;
*p = *q;
*q = temp_val;
}
int main()
{
int x = 45;
int y = 65;
printf( "Initial value of x = %d and y = %d\n", x, y );
swap(&x, &y);
printf( "After swapping said values x = %d and y = %d\n", x, y );
return 0;
}
Copy
Output:
Initial value of x = 45 and y = 65
After swapping said values x = 65 and y = 45
Pointer as Return Value
A pointer can also be returned by functions
In this case, the function returns the memory address of where the value is stored
rather than the value itself
Ensure that you do not return an address to a temporary variable in a function!
Example of Returning a Pointer:
Code: Maximum of two numbers
#include <stdio.h>
int main()
{
int x = 105, y = 102, *ptr;
printf("Original Numbers: x = %d, y = %d",x , y);
ptr = max(&x, &y);
printf("\nMaximum of said two numbers: %d", *ptr);
return 0;
}
Copy
Output:
Original Numbers: x = 105, y = 102
Maximum of said two numbers: 105
nums 54 6 23 45 32 78 89
column
[1] 5 6 7 8
numbers
nums[0] = 0
ptr + 0 = 0
nums[1] = 5
ptr + 1 = 5
nums[2] = 87
ptr + 2 = 87
nums[3] = 32
ptr + 3 = 32
nums[4] = 4
ptr + 4 = 4
nums[5] = 5
ptr + 5 = 5
Passing Arrays as Parameters
Code:
#include <stdio.h>
int sum( int *a, int l) {
int i, s = 0;
for(i = 0; i < l; i++)
s += a[i];
return s;
}
int main()
{
int nums[5] = {1, 2, 3, 4, 5};
int size = sizeof(nums)/sizeof(nums[0]);
printf("Sum is %d\n", sum(nums, size)); //15
return 0;
}
Copy
Output:
Sum is 15
Example: Passing a 2D Array to a Function
In the following example, a 2D array is passed as a parameter, where the second dimension is
specified and the first one is not specified:
Code:
#include <stdio.h>
void test(int N[][4])
{
int i, j;
printf("\n\nPrint the matrix within the test function:");
for (i = 0; i < 4; i++)
{
printf("\n");
for (j = 0; j < 4; j++)
printf("%d ", N[i][j]);
}
printf("\n");
}
int main()
{
int nums[4][4], i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
nums[i][j] = i;
printf("Original Matrix: ");
for (i = 0; i < 4; i++) {
printf("\n");
for (j = 0; j < 4; j++)
printf("%d ", nums[i][j]);
}
test(nums);
}
Copy
In the following example, a pointer to a 2D array is passed as a parameter, where the second
dimension is specified:
Code:
#include <stdio.h>
void test(int (*N)[4])
{
int i, j;
printf("\n\nPrint the matrix within the test function:");
for(i = 0 ; i < 4 ; i++)
{
printf("\n");
for(j = 0 ; j < 4 ; j++)
printf("%d ", N[i][j]);
}
printf("\n");
}
int main()
{
int nums[4][4], i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
nums[i][j] = i;
printf("Original Matrix: ");
for (i = 0; i < 4; i++)
{
printf("\n");
for (j = 0; j < 4; j++)
printf("%d ", nums[i][j]);
}
test(nums);
}
Copy
In the following example, a pointer to a 2D array is passed as a parameter, where the second
dimension is specified:
Code:
#include <stdio.h>
void test(int (*N)[4])
{
int i, j;
printf("\n\nPrint the matrix within the test function:");
for(i = 0 ; i < 4 ; i++)
{
printf("\n");
for(j = 0 ; j < 4 ; j++)
printf("%d ", N[i][j]);
}
printf("\n");
}
int main()
{
int nums[4][4], i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
nums[i][j] = i;
printf("Original Matrix: ");
for (i = 0; i < 4; i++)
{
printf("\n");
for (j = 0; j < 4; j++)
printf("%d ", nums[i][j]);
}
test(nums);
}
Copy
In the following example, a single pointer of a 2D array is passed as parameter:
Code:
#include <stdio.h>
void test(int *N)
{
int i, j;
printf("\n\nPrint the matrix within the test function:");
for(i = 0 ; i < 4 ; i++)
{
printf("\n");
for(j = 0 ; j < 4 ; j++)
printf("%d ", *(N+ 4*i + j));
}
printf("\n");
}
int main()
{
int nums[4][4], i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
nums[i][j] = i;
printf("Original Matrix: ");
for (i = 0; i < 4; i++)
{
printf("\n");
for (j = 0; j < 4; j++)
printf("%d ", nums[i][j]);
}
test(*nums);
}
Copy
Output of the said three programs:
Original Matrix:
0000
1111
2222
3333
Print the matrix within the test function:
0000
1111
2222
3333